php利用header函數(shù)下載各種文件
本文實(shí)例為大家分享了php header函數(shù)下載文件實(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 ();
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- php強(qiáng)制下載文件函數(shù)
- php下載文件的代碼示例
- PHP實(shí)現(xiàn)遠(yuǎn)程下載文件到本地
- php做下載文件的實(shí)現(xiàn)代碼及文件名中亂碼解決方法
- php中強(qiáng)制下載文件的代碼(解決了IE下中文文件名亂碼問(wèn)題)
- php實(shí)現(xiàn)從ftp服務(wù)器上下載文件樹(shù)到本地電腦的程序
- IE php關(guān)于強(qiáng)制下載文件的代碼
- php download.php實(shí)現(xiàn)代碼 跳轉(zhuǎn)到下載文件(response.redirect)
- php下載文件源代碼(強(qiáng)制任意文件格式下載)
- PHP 下載文件時(shí)自動(dòng)添加bom頭的方法實(shí)例
相關(guān)文章
Laravel中的where高級(jí)使用方法實(shí)例講解
這篇文章主要介紹了Laravel中的where高級(jí)使用方法實(shí)例講解,有對(duì)于laravel中的sql中高級(jí)用法感興趣的同學(xué)可以看看文中的實(shí)例學(xué)習(xí)下2021-03-03
ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁(yè)的示例代碼
下面小編就為大家分享一篇ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁(yè)的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
PHP實(shí)現(xiàn)賽郵SUBMAIL簡(jiǎn)單易用短信通知實(shí)例
這篇文章主要介紹了使用PHP實(shí)現(xiàn)賽郵SUBMAIL的簡(jiǎn)單易用短信通知實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Laravel使用模型實(shí)現(xiàn)like模糊查詢的例子
今天小編就為大家分享一篇Laravel使用模型實(shí)現(xiàn)like模糊查詢的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法示例
這篇文章主要介紹了YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了Yii2框架驗(yàn)證碼的基本創(chuàng)建、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03

