PHP下載文件的兩種方法與代碼。
分享PHP實現下載文件的兩種方法。分享下,有用到的朋友看看哦。
方法一:
<?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);?>
以上代碼用到了php header函數,可以參考以下如下的文章:
php header()函數的簡單例子
php header函數實現文件下載的實例代碼
php中header函數的用法舉例詳解
php header 使用詳解
php header函數 文件下載時直接提示保存的代碼
php header函數實現文本文件下載的方法
php 文件頭部(header)信息詳解
php使用header發送各種類型文件下載的例子
瞭解php中header函數的用法。
方法二:
<?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();?>