php 下载文件的两种实现方法(实例代码)

PHP实现下载文件的两种方法。

方法1:

<?php

/**

* 下载文件 header函数应用

* edit by www.jbxue.com

*/

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);

?>

方法二,

<?php

/**

* php 下载文件 header函数与readfile函数

* edit by www.jbxue.com

*/

$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();

 

你可能感兴趣的:(下载文件)