分享---PHP下載檔案的兩種方法

巴扎黑
發布: 2016-11-12 10:46:42
原創
1397 人瀏覽過

PHP下載檔案的兩種方法與程式碼。

分享PHP實作下載檔案的兩種方法。分享下,有用到的朋友看看哦。

方法一:

<?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);
?>
登入後複製

以上程式碼用到了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(&#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();
?>
登入後複製


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!