首页 > php教程 > PHP源码 > php实现中文文件下载

php实现中文文件下载

PHP中文网
发布: 2016-05-24 12:52:55
原创
1255 人浏览过

php代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/*

 * $filepath 文件路径

 * $newfilename 下载时的命名

 * download('/abc.xls','abc教学');

 * 下载下来的文件将是abc教学.xls

 */

function download($filepath,$newfilename){     

        $id = intval($_GET['id']);

        $db = model('tool');

        $tool = $db->where('id='.$id)->find();

        if(!$tool) $this->error('未找到您要查看的工具');

         

        $ua = $_SERVER["HTTP_USER_AGENT"];

         

             

        $pathinfo = pathinfo($filepath);

        $newfilename = $newfilename.'.'.$pathinfo['extension'];

         

        $file = fopen( $filepath ,"r");

 

        header('Content-Type: application/octet-stream');

        header("Accept-Ranges: bytes");

        header("Accept-Length: ".filesize($filepath));

        if (preg_match("/MSIE/", $ua)) {

            header('Content-Disposition: attachment; filename="' . rawurlencode($newfilename)  . '"');

        } else if (preg_match("/Firefox/", $ua)) {

            header('Content-Disposition: attachment; filename*="utf8\'\'' . $newfilename . '"');

        } else {

            header('Content-Disposition: attachment; filename="' . rawurlencode($newfilename) . '"');

        }

         

        echo fread($file, filesize($filepath));

         

        //$db->where('id='.$id)->data($update)->update();

        fclose($file);

 

    }

登录后复制
相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板