我使用php脚本开始下载,它非常简单,看起来像这样:
$dir = 'downloads/';
$type = 'application/x-rar-compressed, application/octet-stream, application/zip';
function makeDownload($file, $dir, $type)
{
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($dir.$file);
}
if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) {
if(file_exists ($dir.$_GET['file'])) {
makeDownload($_GET['file'], $dir, $type);
}
}
它在win7 + ff / opera / chrome / safari上工作正常,但在MAC上它试图下载file.rar.html或file.zip.html而不是file.rar / file.zip。
任何想法为什么?
提前致谢
答案 0 :(得分:0)
“application / x-rar-compressed,application / octet-stream,application / zip”不是有效的文件类型。您需要在脚本中添加逻辑以检测文件类型,然后提供特定的文件类型。示例(未经测试):
<?php
$dir = 'downloads/';
function makeDownload($file, $dir)
{
switch(strtolower(end(explode(".", $file)))) {
case "zip": $type = "application/zip"; break;
case "rar": $type = "application/x-rar-compressed"; break;
default: $type = "application/octet-stream";
}
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($dir.$file);
exit; // you should exit here to prevent the file from becoming corrupted if anything else gets echo'd after this function was called.
}
if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) {
if(file_exists ($dir.$_GET['file'])) {
makeDownload($_GET['file'], $dir);
}
}
?>