从服务器下载的文件(使用download.php)won't打开.为什么?
Downloaded file (using download.php) from server won't open. Why?
我是个初学者。我到处找过,但我不知道为什么通过客户端(Win 7 Firefox)从服务器下载文件后,我无法打开该文件。我试过一个PNG文件和一个MP4文件。下载完成,但文件未打开。这是我的剧本;
$dl_file = $_GET['val']; //Verified the full path and the file name gets passed here
$basename = basename($dl_file);
$ext = pathinfo($dl_file, PATHINFO_EXTENSION);
$length = sprintf("%u", filesize($dl_file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$basename.'');
//manually tried '.$basename.'.PNG' - DID NOT work. How to pass $ext here?
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($dl_file);
我无法想象为什么下载的文件无法打开。它腐败了吗?请对此多加说明。提前谢谢。
经过多次尝试,我发现添加下面显示的2行代码(ob_clean和flush)(和/或添加precheck、postcheck参数)在所有浏览器上都有效。谢谢
if (file_exists($dl_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($dl_file).'"' );
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($dl_file));
ob_clean();
flush();
readfile($dl_file);
exit;
}
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 为什么“;未定义的“;在JavaScript中结束循环
- 为什么这在IE中的工作方式与在Firefox中不同
- 知道为什么我的旋转木马不会自动更改图片吗
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 为什么在变形之前不缺少Fx
- 为什么JavaScript在for循环为3时向所有4发出警报
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 为什么Airbnb风格指南说不鼓励依赖函数名称推断
- 为什么要使用0>javascript中的0
- 为什么无法在TypeScript中导出类实例
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 为什么忽略了eval()代码中的语法错误
- 为什么在画布上画线;t出现
- 为什么js事件消失了
- 为什么won't array[i]. charat (0). touppercase()传递给array[i][
- 从服务器下载的文件(使用download.php)won't打开.为什么?