首頁 >後端開發 >php教程 >php下載檔案的函數範例(圖文)

php下載檔案的函數範例(圖文)

WBOY
WBOY原創
2016-07-25 08:57:04978瀏覽
分享一个php下载函数,用于下载指定的文件,不限文件格式。有需要的朋友参考下。

php下載檔案的函數範例(圖文)举例,如下:

<?php
/**
* php下载文件
* $filename=$_SERVER['DOCUMENT_ROOT']."/test/file/20130503/20130503150036_85945.doc";
* $title="下载测试9";
* down($filename,$title);
* edit by bbs.it-home.org
*/ 
//$filename 必须是路径,不是url
 function down($filename,$title=''){
     $file = fopen($filename,"r"); 
     $filesize = filesize($filename);
     $encoded_filename = urlencode($title);
     $encoded_filename = str_replace("+", "%20", $title);
     $ua = $_SERVER["HTTP_USER_AGENT"];
     if (preg_match("/MSIE/is", $ua)) {  
         $file_name = urlencode($title);
         header("Pragma: public"); header("Expires: 0");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Content-Type: application/force-download");
         header('Content-Type: application/vnd.ms-excel; charset=utf-8');
         header("Content-Transfer-Encoding: binary");
         header('Content-Disposition: attachment; filename='.$file_name);
     } else {
        header('Content-Type: application/octet-stream');
         if (preg_match("/Firefox/is", $ua)) {  
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $title . '"');
         } else {  
            header('Content-Disposition: attachment; filename="' . $title . '"');
         }
     }
    echo fread($file, $filesize);
    fclose($file);
}
?>

运行效果如下图: php下載檔案的函數範例(圖文)



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn