Hướng dẫn php return file
Ở bài trước chúng ta đã học về upload file, hôm nay chúng ta sẽ học về cách Download file sử dụng PHP. Download File trong PHPBình thường bạn không nhất thiết phải sử dụng bất kỳ ngôn ngữ nào như PHP để tải xuống hình ảnh, tệp zip, tài liệu pdf, tệp exe, v.v … Nếu loại tệp đó được lưu trữ trong một thư mục có thể truy cập công khai, bạn chỉ cần tạo một đường link trỏ đến tệp đó và bất cứ khi nào người dùng nhấp vào liên kết, trình duyệt sẽ tự động tải tệp đó xuống. Ví dụ:
Click vào đường link trỏ đến tệp PDF hoặc tệp Image sẽ không tải file xuống máy tính. Nó sẽ chỉ mở file trong trình duyệt. Sau đó bạn có thể lưu nó. Tuy nhiên, các tệp zip và exe thì được tự động tải xuống. Chức năng Download file sử dụng PHPBạn có thể bắt buộc hình ảnh hoặc loại file khác tải trực tiếp xuống máy tính của người dùng bằng hàm readfile() của PHP. Ở đây chúng ta sẽ tạo một bộ sưu tập hình ảnh đơn giản cho phép người dùng tải xuống các tệp hình ảnh từ trình duyệt chỉ bằng một cú click chuột. Hãy tạo một tệp có tên ‘image-gallery.php’ và đặt đoạn mã sau vào trong nó.
Nếu bạn xem mã ví dụ trên một cách cẩn thận, bạn sẽ thấy liên kết tải xuống dẫn đến file ‘download.php’, URL cũng chứa tên tệp hình ảnh dưới dạng chuỗi truy vấn. Ngoài ra, chúng ta đã sử dụng hàm urlencode() của PHP để mã hóa tên tệp hình ảnh để nó có thể được truyền an toàn dưới dạng tham số URL, bởi vì tên tệp có thể chứa các ký tự không an toàn. Đây là mã hoàn chỉnh của tệp ‘download.php‘ để bắt buộc tải xuống hình ảnh.
Tổng kếtNhư vậy, bạn đã biết sử dụng PHP để bắt buộc Download file xuống máy tính người dùng. Bạn cũng có thể buộc tải xuống các định dạng tệp khác như word doc, tệp pdf, v.v. (PHP 4, PHP 5, PHP 7, PHP 8) file — Reads entire file into an array Descriptionfile(string
Parametersfilename Path to the file. Tip A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide. flags The optional parameter FILE_USE_INCLUDE_PATH Search for the file in the include_path. FILE_IGNORE_NEW_LINES Omit newline at the end of each
array element FILE_SKIP_EMPTY_LINES Skip empty lines context A context stream resource. Return Values Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure,
file() returns
Errors/Exceptions Emits an ExamplesExample #1 file() example
NotesWarning When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a See Also
Martin K. ¶ 8 years ago
bingo at dingo dot com ¶ 8 years ago
renanlazarotto at gmail dot com ¶ 1 year ago
d basin ¶ 12 years ago
= file("C:\\Documents and Settings\\myfile.txt"); twichi at web dot de ¶ 10 years ago
$head === true) {
sheldon at hyperlinked dot com ¶ 3 years ago
justin at visunet dot ie ¶ 19 years ago
Reversed: moc dot liamg at senroc dot werdna ¶ 15 years ago
Anonymous ¶ 8 years ago
('UTF-16'); // to help mb_ereg_..() work properly vbchris at gmail dot com ¶ 14 years ago
jon+spamcheck at phpsitesolutions dot com ¶ 14 years ago
lanresmith ¶ 5 years ago
marco dot remy at aol dot com ¶ 8 years ago
$out; info at carstanje dot com ¶ 15 years ago
|