Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Tôi đang tìm kiếm một số hướng dẫn chung về điều này ...

Tôi đã tạo một tập lệnh PHP sử dụng Curl để tải xuống tệp CSV. Hiện tại, nó tải tệp vào máy tính của tôi khi tôi chạy tập lệnh. Tôi muốn sửa đổi đường dẫn tải xuống để định tuyến nó đến một thư mục trên máy chủ web của tôi. Có cách nào dễ dàng để làm điều này với PHP không?

Dưới đây là một bản tóm tắt nhanh chóng về tập lệnh PHP tải xuống tệp CSV:


Để tổng hợp điều này, tôi muốn tập lệnh được sửa đổi để khi tôi chạy tệp PHP này, nó sẽ tải xuống 'file_i_am_doading.csv' vào một thư mục cụ thể trên máy chủ của tôi thay vì tải xuống máy tính trước tiên. Cảm ơn bạn đã dành thời gian và bất kỳ hướng nào bạn có thể cung cấp đều được đánh giá cao.

hỏi ngày 28 tháng 7 năm 2011 lúc 16:50Jul 28, 2011 at 16:50

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

1

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);

Đã trả lời ngày 28 tháng 7 năm 2011 lúc 16:52Jul 28, 2011 at 16:52

Marc Bmarc bMarc B

352K42 Huy hiệu vàng405 Huy hiệu bạc488 Huy hiệu đồng42 gold badges405 silver badges488 bronze badges

3

$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})

Mã của bạn đang gửi lại byte của tệp bạn muốn tải xuống. Sau đó, bạn đang cố gắng hiển thị các byte đó trong một alert, chỉ có thể hiển thị văn bản. Đó là lý do tại sao bạn nhận được một thông điệp đầy vô nghĩa - đó là JavaScript cố gắng hết sức để giải thích các byte như một chuỗi.

Bạn không thể sử dụng yêu cầu AJAX để tải xuống một tệp. Vì bạn yêu cầu yêu cầu POST, bạn sẽ cần tạo một

ẩn ẩn vào trang tải xuống của bạn và gửi nó từ mã của bạn.

Ngoài ra, thay đổi mã download.php của bạn để hoạt động với yêu cầu GET, thêm tên tệp vào chuỗi truy vấn trên liên kết của bạn và thêm thuộc tính download [^] vào liên kết.

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
2
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
0
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
5
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
5
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
3

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
9
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1alert1____
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
8
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
2alert4alert5
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8
: The file_get_contents() function is used to read a file into a string. This function uses memory mapping techniques that are supported by the server and thus enhances the performance making it a preferred way of reading the contents of a file.

Syntax:

file_get_contents($path, $include_path, $context, 
                  $start, $max_length)

Ví dụ 1: Ví dụ này minh họa việc sử dụng hàm file_get_contents () để đọc tệp thành một chuỗi.This example illustrates the use of file_get_contents() function to read the file into a string.

PHP

Output:

Trước khi chạy chương trình: & nbsp;

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Thư mục nguồn PHP

Sau khi chạy chương trình:

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Tệp được tải xuống sau khi thực hiện thành công

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Tải xuống tệp hình ảnh

Sử dụng PHP Curl: & nbsp; Curl là viết tắt của ‘máy khách cho URL, ban đầu với URL được đánh vần ở chữ hoa để làm cho rõ ràng rằng nó liên quan đến URL. Nó được phát âm là ‘xem URL. Dự án Curl có hai sản phẩm libcurl và curl.:  The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. The cURL project has two products libcurl and curl.

Các bước để tải xuống thefile: & nbsp; & nbsp; file:  

  • Khởi tạo một URL tệp vào biến.
  • Tạo phiên Curl.
  • Khai báo một biến và lưu trữ tên thư mục trong đó tệp đã tải xuống sẽ lưu.
  • Sử dụng hàm basename () để trả về tên tệp basename nếu đường dẫn tệp được cung cấp dưới dạng tham số.
  • Lưu tệp vào vị trí đã cho.
  • Mở vị trí tệp đã lưu trong chế độ Chuỗi ghi.
  • Đặt tùy chọn để chuyển curl.
  • Thực hiện phiên Curl và đóng phiên Curl và miễn phí tất cả các tài nguyên.
  • Đóng tệp.

Ví dụ: Ví dụ này minh họa việc sử dụng Curl PHP để thực hiện các yêu cầu HTTP trong PHP, để tải xuống tệp.This example illustrates the use of the PHP Curl to make HTTP requests in PHP, in order to download the file.

PHP

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
0

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
2
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
3

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
5
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
6______12

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
0
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
2223

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
5
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
7
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
8__12

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
2
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
0
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
5
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
5
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
3

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
9
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
1alert1____
$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})
8
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
2alert4alert5
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1alert8
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
5POST0
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
9
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1alert8
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
5POST6

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
1POST8
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
5
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
12
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
5
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8

$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
16
file_get_contents($path, $include_path, $context, 
                  $start, $max_length)
9
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
8

9

Output:

Trước khi chạy chương trình:

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Thư mục nguồn PHP

Sau khi chạy chương trình:

Hướng dẫn how do i set php to download to a specific directory? - làm cách nào để cài đặt php để tải xuống một thư mục cụ thể?

Tải xuống tệp hình ảnh

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo hướng dẫn PHP và các ví dụ PHP này.


Làm thế nào để lưu tệp trong thư mục cụ thể trong PHP?

PHP $ target_path = "hình ảnh/"; $ target_path = $ target_path.basename ($ _files ['userFile'] ['name']); Move_upLoaded_File ($ _Files ['userFile'] ['tmp_name'], $ target_path); ?> Khi tệp (hình ảnh) được lưu tại đường dẫn được chỉ định ... điều gì sẽ xảy ra nếu tôi muốn lưu tệp với một số tên mong muốn ....$target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

Làm thế nào tải xuống tất cả các tệp từ thư mục trong PHP?

PHP $ name = $ _get ['nama'];Tải xuống ($ name);Tải xuống chức năng ($ name) {$ file = $ nama_fail;if (file_exists ($ file)) {header ('nội dung mô tả: chuyển tệp');Tiêu đề ('loại nội dung: Ứng dụng/-dòng octet');Tiêu đề ('Xác định nội dung: tệp đính kèm; fileName ='.

Làm cách nào để làm cho tệp PHP của tôi có thể tải xuống?

Các bước để tải xuống tệp:..
Khởi tạo URL tệp vào biến ..
Tạo phiên Curl ..
Khai báo một biến và lưu trữ tên thư mục trong đó tệp đã tải xuống sẽ lưu ..
Sử dụng hàm basename () để trả về tên tệp basename nếu đường dẫn tệp được cung cấp dưới dạng tham số ..
Lưu tệp vào vị trí đã cho ..

Làm cách nào để đặt đường dẫn để tải xuống?

Nhấp chuột phải vào nút Bắt đầu, sau đó chọn File Explorer ...
Nhấp chuột phải vào vị trí tải xuống ở khung bên trái, sau đó chọn Thuộc tính ..
Điều hướng đến tab Vị trí, nhấp vào nút Di chuyển, sau đó chọn thư mục nơi các tệp sẽ được tải xuống và nhấp vào Chọn thư mục.....
WARNING!.