Làm cách nào để tải xuống tệp từ mã php?

Tóm lược. trong hướng dẫn này, bạn sẽ học cách tải xuống một tệp trong PHP bằng cách sử dụng hàm readfile()

Giới thiệu về hàm readfile() trong PHP

Hàm readfile() đọc dữ liệu từ một tệp và ghi nó vào bộ đệm đầu ra

Đây là cú pháp của hàm readfile()

readfile ( string $filename , bool $use_include_path = false , resource $context = ? ) : int|false

Code language: PHP (php)

Hàm readfile() có các tham số sau

  • $filename là đường dẫn đến tệp
  • $use_include_path nếu được đặt thành

    $filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

    Code language: HTML, XML (xml)
    0, chức năng sẽ tìm kiếm tệp trong đường dẫn bao gồm
  • $filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

    Code language: HTML, XML (xml)
    0 chỉ định bối cảnh luồng

Hàm readfile() trả về số byte nếu nó đọc thành công dữ liệu từ tệp hoặc

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
2 nếu nó không đọc được

Ví dụ về tệp tải xuống PHP

Ví dụ sau đây cho thấy cách tải xuống tệp

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
3 bằng cách sử dụng ví dụ về hàm readfile()

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
3

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
4

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
5

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
6

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
7

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
8

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
9

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
0

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
1

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
2

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
40

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
41

$filename = 'readme.pdf'; if (file_exists($filename)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); exit; }

Code language: HTML, XML (xml)
42

Làm cách nào để tải xuống tệp từ máy chủ PHP?

Các bước để tải tệp xuống. .
Khởi tạo URL tệp cho biến
Tạo phiên cURL
Khai báo một biến và lưu tên thư mục lưu file tải về
Sử dụng hàm basename() để trả về tên cơ sở tệp 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ải xuống tệp PDF trong mã PHP?

$file = $_GET [ "file" ]. ". pdf" ; header( "Bố trí nội dung. tập tin đính kèm; .

Bạn có thể tải xuống tệp PHP từ một trang web không?

Php là ngôn ngữ phía máy chủ, nó luôn thực thi trên máy chủ và trả về html. bạn có thể sao chép hoặc tải xuống bất kỳ tệp php nào cho đến khi có quyền truy cập ftp .

Làm cách nào để tải xuống tệp trong PHP w3schools?

Có thể tải xuống một tệp trong PHP bằng Hàm readfile() của PHP . Hàm readfile() trong PHP là một hàm dựng sẵn đã được định nghĩa trong thư viện PHP. Hàm đọc tệp PHP (). Hàm readfile() trong PHP được sử dụng để đọc một tệp và ghi nó vào bộ đệm đầu ra.