0

我从一个 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。

任何想法为什么?

提前致谢

4

1 回答 1

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);
    }

}
?>
于 2013-07-26T22:01:10.417 回答