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('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($filepath)); header('Content-Transfer-Encoding: binary'); header('Expires: 0′);header('Cache-Control: must-revalidate, post-check=0, pre-check=0′); header('Pragma: public'); header('Content-Length: ' . 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('Content-type: application/x-'.$fileinfo['extension']); header('Content-Disposition: attachment; filename='.$fileinfo['basename']); header('Content-Length: '.filesize($filename)); readfile($thefile);exit(); ?>