首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用php脚本下载- .rar变成mac上的.rar.html

用php脚本下载- .rar变成mac上的.rar.html
EN

Stack Overflow用户
提问于 2013-07-26 21:59:02
回答 1查看 2.1K关注 0票数 0

我从php脚本开始下载,它非常简单,如下所示:

代码语言:javascript
复制
$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。

知道为什么吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-26 22:01:10

“application/x压缩的应用程序/八位流,application/zip”不是有效的文件类型。您需要在脚本中添加逻辑以检测文件类型,然后提供特定的文件类型。示例(未经测试):

代码语言:javascript
复制
<?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);
    }

}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17891693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档