求帮看下这段PHP下载MP4文件的问题,文件下载不完整

php中文网
发布: 2016-06-23 13:39:30
原创
1609人浏览过

function dl_file($file){    $len = filesize($file);    $filename = basename($file);    $file_extension = strtolower(substr(strrchr($filename,"."),1));    switch( $file_extension ) {    case "mp4": $ctype="video/mp4"; break;    default: $ctype="application/force-download";    }    header("Pragma: public");    header("Expires: 0");    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");    header("Cache-Control: public");     header("Content-Description: File Transfer");    header("Content-Type: $ctype");    $header="Content-Disposition: attachment; filename=".$filename."";    header($header );    header("Content-Transfer-Encoding: binary");    header("Content-Length: ".$len);	ob_clean();    flush();    readfile($file);    exit;}dl_file('test.mp4');
登录后复制


在火狐,谷歌浏览器下载正常,IE和迅雷下载文件不完整

文小言
文小言

百度旗下新搜索智能助手,有问题,问小言。

文小言 57
查看详情 文小言

回复讨论(解决方案)

ob_clean();
    flush();
    readfile($file);

ob_clean();
    flush();
    readfile($file);


有这句呀

没加mime头

    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
这俩删掉试试

没加mime头



MIME类型吗?加那,IIS吗

    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
这俩删掉试试





$len = filesize($file);
header("Content-Length: ".$len);
上面2句删除后可以下载,但没统计文件大小,迅雷和IE没显示下载时长。

nginx + php 环境下 没有你说的那个问题,ie和迅雷都可以正常下载 不过我的文件是flv,这样的话也只能说明是mime头出错了
我的代码如下:

function dl_file($file){
    $len = filesize($file);
    $filename = basename($file);
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    switch( $file_extension ) {
    case "mp4": $ctype="video/mp4"; break;
    default: $ctype="application/force-download";
    }
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer");
    header("Content-Type: $ctype");
    $header="Content-Disposition: attachment; filename=".$filename."";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    ob_clean();
    flush();
    readfile($file);
    exit;
}
dl_file('http://res.805.com/videos/霍山·上林后苑(易仟亿集团) [640x480].flv'); 只改了一个文件名而已  其他的都没变

nginx + php 环境下 没有你说的那个问题,ie和迅雷都可以正常下载 不过我的文件是flv,这样的话也只能说明是mime头出错了
我的代码如下:

function dl_file($file){
    $len = filesize($file);
    $filename = basename($file);
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    switch( $file_extension ) {
    case "mp4": $ctype="video/mp4"; break;
    default: $ctype="application/force-download";
    }
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer");
    header("Content-Type: $ctype");
    $header="Content-Disposition: attachment; filename=".$filename."";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    ob_clean();
    flush();
    readfile($file);
    exit;
}
dl_file('http://res.805.com/videos/霍山·上林后苑(易仟亿集团) [640x480].flv'); 只改了一个文件名而已  其他的都没变



IE ,迅雷 下载有显示文件大小吗?

没测试过 应该有吧 有"Content-Length头的

你可以去看下ucenter文档

博客看看
http://www.jb51.net/article/46440.htm

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号