PHP檔案下載功能 在ThinkPHP中實現步驟
阿新 • • 發佈:2018-12-14
下載功能
1.如果是其他檔案,直接在a標籤的href屬性中填寫路徑,可以下載。
2.如果是一個圖片檔案,那麼直接在a標籤的href屬性中,填寫路徑是不能進行下載。
那麼需要進行一下操作步驟:
PHP檔案下載固定4句話:header("Content-type: application/octet-stream"); header('Content-Disposition: attachment; filename="'所需的引數是:檔案路徑. basename($path) .'"'); header("Content-Length: ". filesize($path)); readfile($path);
思路:建立Doc/download方法,在方法中獲取要下載的檔案路徑,呼叫上面4句話進行下載
1) 修改檔案下載地址,將檔案的doc_id傳遞到download方法中
2) 建立Doc/download方法,接收doc_id,根據id查詢對應的路徑,再呼叫4句下載函式,進行下載操作。
function download(){ //接收公文id $id = I('get.id'); //根據公文id查詢對應的附件路徑 $data = D('Doc')->field('doc_file')->find($id); $path = $data['doc_file']; header("Content-type: application/octet-stream"); header('Content-Disposition: attachment; filename="'. basename($path) .'"'); header("Content-Length: ". filesize($path)); readfile($path); }