Home >Backend Development >PHP Tutorial >Share---Two ways to download files in PHP

Share---Two ways to download files in PHP

巴扎黑
巴扎黑Original
2016-11-12 10:46:421494browse

Two methods and codes for downloading files in PHP.

Share two methods of downloading files in PHP. Share it so friends who may find it useful can take a look.

Method 1:

<?php
/*** 下载文件* header函数**/header(&#39;Content-Description: File Transfer&#39;);
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename=&#39;.basename($filepath));
header(&#39;Content-Transfer-Encoding: binary&#39;);
header(&#39;Expires: 0′);header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(&#39;Pragma: public&#39;);
header(&#39;Content-Length: &#39; . filesize($filepath));
readfile($file_path);
?>

The above code uses the php header function. You can refer to the following articles:
A simple example of the php header() function
Example code of the php header function to implement file download
Examples of usage of the header function in php Detailed explanation
php header Detailed explanation of usage
php header function directly prompts the saved code when downloading the file
php header function method to implement text file download
php file header (header) detailed explanation
php examples of using header to send various types of file downloads

Understand the usage of header function in php.

Method 2:

<?php//文件下载//readfile
$fileinfo = pathinfo($filename);
header(&#39;Content-type: application/x-&#39;.$fileinfo[&#39;extension&#39;]);
header(&#39;Content-Disposition: attachment; filename=&#39;.$fileinfo[&#39;basename&#39;]);
header(&#39;Content-Length: &#39;.filesize($filename));
readfile($thefile);exit();
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn