php利用header函數(shù)下載各種文件
本文實例為大家分享了php header函數(shù)下載文件實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
http://www.php.net/manual/en/function.readfile.php
<?php /** * 下載文件 * header函數(shù) * */ dl_file($_GET ['filename']); function dl_file($file) { $file = ".//images//" . $file; //First, see if the file exists if (! is_file ( $file )) { die ( "<b>404 File not found!</b>" ); } // Gather relevent info about file $len = filesize ( $file ); $filename = basename ( $file ); $file_extension = strtolower ( substr ( strrchr ( $filename, "." ), 1 ) ); // This will set the Content-Type to the appropriate setting for the file switch ($file_extension) { case "pdf" : $ctype = "application/pdf"; break; case "exe" : $ctype = "application/octet-stream"; break; case "zip" : $ctype = "application/zip"; break; case "doc" : $ctype = "application/msword"; break; case "xls" : $ctype = "application/vnd.ms-excel"; break; case "ppt" : $ctype = "application/vnd.ms-powerpoint"; break; case "gif" : $ctype = "image/gif"; break; case "png" : $ctype = "image/png"; break; case "jpeg" : case "jpg" : $ctype = "image/jpg"; break; case "mp3" : $ctype = "audio/mpeg"; break; case "wav" : $ctype = "audio/x-wav"; break; case "mpeg" : case "mpg" : case "mpe" : $ctype = "video/mpeg"; break; case "mov" : $ctype = "video/quicktime"; break; case "avi" : $ctype = "video/x-msvideo"; break; // The following are for extensions that shouldn't be downloaded // (sensitive stuff, like php files) case "php" : case "htm" : case "html" : case "txt" : die ( "<b>Cannot be used for " . $file_extension . " files!</b>" ); break; default : $ctype = "application/force-download"; } $file_temp = fopen ( $file, "r" ); // Begin writing headers header ( "Pragma: public" ); header ( "Expires: 0" ); header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header ( "Cache-Control: public" ); header ( "Content-Description: File Transfer" ); // Use the switch-generated Content-Type header ( "Content-Type: $ctype" ); // Force the download $header = "Content-Disposition: attachment; filename=" . $filename . ";"; header ( $header ); header ( "Content-Transfer-Encoding: binary" ); header ( "Content-Length: " . $len ); //@readfile ( $file ); echo fread ( $file_temp, filesize ( $file ) ); fclose ( $file_temp ); exit (); } ?>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法
這篇文章主要介紹了php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家使用的時候注意方法,需要的朋友可以參考下2017-10-10利用php + Laravel如何實現(xiàn)部署自動化詳解
這篇文章主要給大家介紹了關(guān)于利用php + Laravel如何實現(xiàn)部署自動化的相關(guān)資料,文中通過示例代碼介紹非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10php實現(xiàn)微信小程序訂閱消息推送(access_token獲取緩存刷新)
這篇文章主要為大家介紹了php實現(xiàn)微信小程序訂閱消息推送包含access_token獲取緩存刷新示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08ThinkPHP框架整合微信支付之Native 掃碼支付模式一圖文詳解
這篇文章主要介紹了ThinkPHP框架整合微信支付之Native 掃碼支付模式一,結(jié)合圖文形式詳細分析了thinkPHP整合微信支付接口的掃碼支付功能相關(guān)操作步驟、實現(xiàn)技巧與注意事項,需要的朋友可以參考下2019-04-04淺談使用 PHP 進行手機 APP 開發(fā)(API 接口開發(fā))
做過 API 的人應(yīng)該了解,其實開發(fā) API 比開發(fā) WEB 更簡潔,但可能邏輯更復雜,因為 API 其實就是數(shù)據(jù)輸出,不用呈現(xiàn)頁面,所以也就不存在 MVC(API 只有 M 和 C),那么我們來探討下,如何使用php進行手機API接口開發(fā)2014-08-08