教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php下载文件的函数示例(图文)

php下载文件的函数示例(图文)

发布时间:2016-10-13   编辑:jiaochengji.com
分享一个php下载函数,用于下载指定的文件,不限文件格式。有需要的朋友参考下。

php下载文件的函数举例,如下:

<?php
/**
* php下载文件
* $filename=$_SERVER['DOCUMENT_ROOT']."/test/file/20130503/20130503150036_85945.doc";
* $title="下载测试9";
* down($filename,$title);
* edit by www.jbxue.com
*/ 
//$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下载文件的函数

您可能感兴趣的文章:
php绘图不显示图片怎么办
php下载文件的函数示例(图文)
php使用ftp下载文件的简单例子
php点击链接下载图片或其它类型文件的实例代码
php下载css中图片函数
php ftp下载文件的代码一例
php文件下载(防止中文文件名乱码)的示例代码
PHP 强制下载文件示例代码
php实现文件下载实例代码
php中如何显示图片?

[关闭]
~ ~