PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php文件下载函数

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:php文件下载函数,$filepath 文件路径 ,$filename 文件名称

    1. /** 
    2.      * 文件下载 
    3.      * @param $filepath 文件路径 
    4.      * @param $filename 文件名称 
    5.      *@B5教程网 www.bcty365.com 
    6.      */ 
    7.     private function downLocalFile($filepath$filename = ''
    8.     { 
    9.         if (!$filename) { 
    10.             $filename = basename($filepath); 
    11.         } 
    12.  
    13.         $doc_path = str_ireplace(str_replace("\\", "/", $_SERVER['SCRIPT_NAME']), ''$_SERVER['SCRIPT_FILENAME']); 
    14.         $realpath = $doc_path . $filepath
    15.         if (!file_exists($realpath)) { 
    16.             header('HTTP/1.1 404 Not Found'); 
    17.             echo "Error: 404 Not Found.(server file path error)<!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding -->"
    18.             exit
    19.         } 
    20.  
    21.         $filetype = strtolower(substr(strrchr($filename"."), 1)); //获取文件扩展名 
    22.         $filesize = sprintf("%u"filesize($realpath)); 
    23.         if (ob_get_length() !== false) { 
    24.             @ob_end_clean(); 
    25.         } 
    26.  
    27.         header('Pragma: public'); 
    28.         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
    29.         header('Cache-Control: no-store, no-cache, must-revalidate'); 
    30.         header('Cache-Control: pre-check=0, post-check=0, max-age=0'); 
    31.         header('Content-Transfer-Encoding: binary'); 
    32.         header('Content-Encoding: none'); 
    33.         header('Content-type: ' . $filetype); 
    34.         if (preg_match('/MSIE/'$_SERVER['HTTP_USER_AGENT'])) { 
    35.             //for IE 
    36.             header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"'); 
    37.         } else { 
    38.             header('Content-Disposition: attachment; filename="' . $filename . '"'); 
    39.         } 
    40.         header('Content-length: ' . $filesize); 
    41.         readfile($realpath); 
    42.         exit
    43.     } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-5585-1.html
    相关热词搜索: php文件下载