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

來源:互聯網
上載者:User
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();?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.