PHP实现下载文件?

 $length = filesize($file);
                $type = mime_content_type($file);
                $showname =  ltrim(strrchr($file,'/'),'/');
                header("Content-Description: File Transfer");
                header('Content-type: ' . $type);
                header('Content-Length:' . $length);
                if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
                    header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
                } else {
                    header('Content-Disposition: attachment; filename="' . $showname . '"');
                }
                readfile($file);

$file 是指你的文件路径名 

mime_content_type 意思是检测你的文件类型(如果出现这个函数错误,可以检查一下是否相应的模块开启了没?也可以舍去这行代码)

ltrim()  移除字符串左侧的空白字符或其他预定义字符。

preg_match  执行匹配正则表达式

readfile  输出文件

猜你喜欢

转载自blog.csdn.net/wqzbxh/article/details/83747267
今日推荐