Hướng dẫn php auto download file - tự động tải xuống tệp php

Nói chung, không có tập lệnh PHP nào được yêu cầu để tải xuống một tệp với các tiện ích mở rộng EXE và ZIP. Nếu vị trí tệp của loại tệp này được đặt trong thuộc tính href của phần tử neo, thì tệp sẽ tự động tải xuống khi người dùng nhấp vào liên kết tải xuống. Một số tệp, chẳng hạn như tệp hình ảnh, tệp PDF, tệp văn bản, tệp CSV, v.v., không tự động tải xuống và thay vào đó, mở trong trình duyệt khi người dùng nhấp vào liên kết tải xuống. Các tệp này có thể được tải xuống cưỡng bức trong PHP bằng cách sử dụng hàm readfile [] không tự động tải xuống. Hướng dẫn này chỉ cho bạn cách buộc tải xuống bất kỳ tệp nào bằng tập lệnh PHP.exe and zip. If the file location of this type of file is set in the href attribute of the anchor element, then the file automatically downloads when the user clicks on the download link. Some files, such as image files, PDF files, text files, CSV files, etc., do not download automatically, and instead, open in the browser when the user clicks on the download link. These files can be downloaded forcibly in PHP using the readfile[] function that does not download automatically. This tutorial shows you how to forcibly download any file using PHP script.exe and zip. If the file location of this type of file is set in the href attribute of the anchor element, then the file automatically downloads when the user clicks on the download link. Some files, such as image files, PDF files, text files, CSV files, etc., do not download automatically, and instead, open in the browser when the user clicks on the download link. These files can be downloaded forcibly in PHP using the readfile[] function that does not download automatically. This tutorial shows you how to forcibly download any file using PHP script.

Nội dung chính ShowShow

  • Kiểm tra liên kết tải xuống
  • Tải xuống tệp bằng hàm readfile []
  • Ví dụ 1: Tải xuống tệp với tên tệp
  • Ví dụ 2: Tải xuống tệp với đường dẫn tệp
  • Video hướng dẫn
  • Sự kết luận
  • Thông tin về các Tác giả
  • Làm cách nào để tải xuống tệp pdf từ máy chủ PHP?
  • Tôi có thể tải xuống tệp PHP từ trang web không?
  • Làm cách nào để tải hình ảnh từ mã PHP?
  • Làm thế nào để bạn tải nội dung của một tập lệnh một PHP sang một tập lệnh khác?

Kiểm tra liên kết tải xuống

Tải xuống tệp bằng hàm readfile []zip and exe files download automatically, without using PHP script. First, create an HTML file with the following code. Here, the four anchor elements are defined to download the four types of files. These file types include TEXT, ZIP, PDF, and JPG files.

Download.html

Ví dụ 1: Tải xuống tệp với tên tệphtml>

Download Files


Download TEXT file
Download ZIP file
Download PDF file
Download JPG file

Ví dụ 2: Tải xuống tệp với đường dẫn tệp
The following dialog box will appear to download the file after clicking the zip file link. The user can then download the file or open the file in the archive manager.


Video hướng dẫnreadfile[] function.

Tải xuống tệp bằng hàm readfile []

Ví dụ 1: Tải xuống tệp với tên tệpreadfile[] function is used in PHP script to forcibly download any file of the current location, or the file with the file path. The syntax of this function is given below.

Ví dụ 2: Tải xuống tệp với đường dẫn tệp
int readfile [ string $filename [, bool $use_include_path = false [, resource $context ]] ]

Video hướng dẫn$filename, stores the filename or filename with the path that will download. The default value of the second parameter, $use_include_path, is false and will be set to true if the filename with the path is used in the first argument. The third argument, $context, is used to indicate the context stream resource. This function returns the number of bytes read from the file mentioned in the first argument. The uses of this function are shown in the following two examples.

Ví dụ 1: Tải xuống tệp với tên tệp

Trong ví dụ này, chúng tôi sẽ tạo một tệp HTML với mã sau, trong đó tên tệp sẽ được truyền dưới dạng tham số của đường dẫn URL có tên và giá trị của tham số này sẽ được chuyển đến tệp PHP có tên Download.php.path, and the value of this parameter will be passed to the PHP file named download.php.path, and the value of this parameter will be passed to the PHP file named download.php.

download2.html

Tải xuống tệp tải xuống tệp văn bản Tải xuống tệp zip tải xuống tệp pdf tải xuống tệp jpghtml> Download Files Download TEXT file Download ZIP file Download PDF file Download JPG file html>

Download Files


Download TEXT file
Download ZIP file
Download PDF file
Download JPG file

Chúng tôi sẽ tạo tệp PHP với mã sau để tải xuống tệp. Ở đây, hàm isset [] được sử dụng để kiểm tra xem $ _get [‘đường dẫn] có được xác định hay không. Nếu biến được xác định, hàm file_exists [] được sử dụng để kiểm tra xem tệp có tồn tại trong máy chủ hay không. Tiếp theo, hàm tiêu đề [] được sử dụng để đặt thông tin tiêu đề cần thiết trước khi sử dụng hàm readFile []. Hàm basename [] được sử dụng để truy xuất tên tệp và hàm fileSize [] được sử dụng để đọc kích thước của tệp trong byte, sẽ được hiển thị trong hộp thoại mở để tải xuống tệp. Hàm Flush [] được sử dụng để xóa bộ đệm đầu ra. Hàm readFile [] chỉ được sử dụng với tên tệp, ở đây.isset[] function is used to check whether the $_GET[‘path’] is defined. If the variable is defined, the file_exists[] function is used to check whether the file exists in the server. Next, the header[] function is used to set the necessary header information before using the readfile[] function. The basename[] function is used to retrieve the filename, and the filesize[] function is used to read the size of the file in bytes, which will be shown in the opening dialog box to download the file. The flush[] function is used to clear the output buffer. The readfile[] function is used with the filename only, here.isset[] function is used to check whether the $_GET[‘path’] is defined. If the variable is defined, the file_exists[] function is used to check whether the file exists in the server. Next, the header[] function is used to set the necessary header information before using the readfile[] function. The basename[] function is used to retrieve the filename, and the filesize[] function is used to read the size of the file in bytes, which will be shown in the opening dialog box to download the file. The flush[] function is used to clear the output buffer. The readfile[] function is used with the filename only, here.

download.php

if [isset [$ _ get ['path']]] {// đọc tên tệp $ fileName = $ _ get ['path']; // kiểm tra tệp tồn tại hoặc notif [file_exists [$ filename]] {[isset[$_GET['path']]]{//Read the filename$filename=$_GET['path'];//Check the file exists or notif[file_exists[$filename]]{[isset[$_GET['path']]]
{
//Read the filename
$filename = $_GET['path'];
//Check the file exists or not
if[file_exists[$filename]] {

// Xác định thông tin tiêu đề ['Description: Truyền tệp']; Tiêu đề ['Loại nội dung: Ứng dụng/Octet-Stream']; "Hết hạn: 0"]; Tiêu đề ['Xác định nội dung: tệp đính kèm; fileName = "'. Basename [$ fileName]. '"']; Tiêu đề ['độ dài nội dung:' .filesize [$ fileName]]; tiêu đề [tiêu đề [ 'Pragma: công khai'];header['Content-Description: File Transfer'];header['Content-Type: application/octet-stream'];header["Cache-Control: no-cache, must-revalidate"];header["Expires: 0"];header['Content-Disposition: attachment; filename="'.basename[$filename].'"'];header['Content-Length: '.filesize[$filename]];header['Pragma: public'];
header['Content-Description: File Transfer'];
header['Content-Type: application/octet-stream'];
header["Cache-Control: no-cache, must-revalidate"];
header["Expires: 0"];
header['Content-Disposition: attachment; filename="'.basename[$filename].'"'];
header['Content-Length: ' . filesize[$filename]];
header['Pragma: public'];

// Xóa bộ đệm đầu ra hệ thống [];flush[];
flush[];

// đọc kích thước của filereadfile [$ fileName];readfile[$filename];
readfile[$filename];

// chấm dứt từ scriptdie [];} other {echo "tệp không tồn tại.";}} Usedie[];}else{echo"File does not exist.";}}elseecho"Filename is not defined."?>
die[];
}
else{
echo "File does not exist.";
}
}
else
echo "Filename is not defined."
?>

Đầu ra đầu ra sau sẽ xuất hiện sau khi nhấp vào liên kết tải xuống của tệp hình ảnh. Kích thước tệp của hình ảnh Rose.jpg là 27,2 kb, như thể hiện trong hộp thoại. Bạn có thể tải xuống tệp bằng cách chọn nút Save File Radio và nhấn nút OK. The following output will appear after clicking the download link of the image file. The file size of the rose.jpg image is 27.2 KB, as shown in the dialog box. You can download the file by selecting the Save File radio button and pressing the OK button.
The following output will appear after clicking the download link of the image file. The file size of the rose.jpg image is 27.2 KB, as shown in the dialog box. You can download the file by selecting the Save File radio button and pressing the OK button.

Ví dụ 2: Tải xuống tệp với đường dẫn tệp

Nếu tệp tồn tại tại vị trí tệp đã cho, đường dẫn tệp sẽ được yêu cầu đề cập trong URL. Trong ví dụ này, chúng tôi sẽ tạo một tệp HTML với mã sau, sẽ truyền tên tệp với đường dẫn tệp:

download3.html

Tải xuống tệp tải xuống tệp pdf filedown tải tệp jpghtml>Download FilesDownload PDF fileDownload JPG filehtml>

Download Files


Download PDF file
Download JPG file

Chúng tôi sẽ tạo một tệp PHP với mã sau để tải xuống một tệp từ đường dẫn tệp. Mã PHP trong ví dụ trước sẽ được sửa đổi một chút để tải xuống tệp từ đường dẫn đã cho. Hàm ClearStateCache [] được sử dụng để xóa bộ đệm được lưu trữ trước đó. Hai đối số được sử dụng trong hàm readfile [].clearstatecache[] function is used to clear the cache that was previously stored. Two arguments are used in the readfile[] function.clearstatecache[] function is used to clear the cache that was previously stored. Two arguments are used in the readfile[] function.

download2.php


if[isset[$_GET['path']]]{//Read the url$url=$_GET['path'];[isset[$_GET['path']]]
{
//Read the url
$url = $_GET['path'];

// Xóa CacheclearStatCache [];clearstatcache[];
clearstatcache[];

// kiểm tra đường dẫn tệp tồn tại hoặc notif [file_exists [$ url]] {if[file_exists[$url]]{
if[file_exists[$url]] {

// Xác định thông tin tiêu đề ['D-Description: Truyền tệp']; Tiêu đề ['Loại nội dung: Ứng dụng/Octet-Stream']; .header['Content-Description: File Transfer'];header['Content-Type: application/octet-stream'];header['Content-Disposition: attachment; filename="'.basename[$url].'"'];header['Content-Length: '.filesize[$url]];header['Pragma: public'];
header['Content-Description: File Transfer'];
header['Content-Type: application/octet-stream'];
header['Content-Disposition: attachment; filename="'.basename[$url].'"'];
header['Content-Length: ' . filesize[$url]];
header['Pragma: public'];

// Xóa bộ đệm đầu ra hệ thống [];flush[];
flush[];

// đọc kích thước của filereadfile [$ url, true];readfile[$url,true];
readfile[$url,true];

// chấm dứt từ scriptdie [];} other {echo "đường dẫn tệp không tồn tại.";}} Echo "đường dẫn tệp không được xác định."die[];}else{echo"File path does not exist.";}}echo"File path is not defined."
die[];
}
else{
echo "File path does not exist.";
}
}
echo "File path is not defined."

?>

Đầu ra Sau khi liên kết tải xuống của tệp PDF được nhấp, đầu ra sau sẽ xuất hiện. After the download link of the PDF file is clicked, the following output will appear.
After the download link of the PDF file is clicked, the following output will appear.

Video hướng dẫn

Sự kết luận

Bài viết này cung cấp một cách đơn giản để buộc tải xuống bất kỳ tệp nào bằng tập lệnh PHP, để giúp người đọc thêm tính năng tải xuống trong tập lệnh của họ.

a-z] $/i ', $ file]] {$ filePath = "hình ảnh/".Tệp $;// Quá trình tải xuống if [file_exists [$ filePath]] {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 = "'.

Tệp [$ user. $ format]];ReadFile [$ user. $ Định dạng]; PHP.$format]]; readfile[$user.$format];php. $format]]; readfile[$user. $format]; php.

Bài Viết Liên Quan

Chủ Đề