dql7588 2016-04-04 16:42
浏览 58
已采纳

PHP - 从外部服务器下载文件,文件名有引号“'”:'filename.zip'

I made a script where depending on the request the server will return a specific file for download from an external source, I use an external source because I have unlimited bandwidth on the other server:

$ch = curl_init();
$url="http://www.example.com/downloads/$fileName";
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // make it a HEAD request
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);

$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mimeType");
header("Content-length: $size");
header("Content-Transfer-Encoding: binary"); 
header("Content-Disposition:attachment;filename='$fileName'");
readfile($url);

$filename comes from the request on the front end. It is a simple post from a form.

Everything works great, but some users, not all, have reported that they cant open the file because the file name has quotes: 'filename.zip', instead of filename.zip

I hit a wall, I have no idea even where to start looking, apparently this happens with some mac users. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongshen2903 2016-04-04 16:53
    关注

    The HTTP standard would have you putting double quotes around the filename parameter in the Content-Disposition header. That might look like this in your code:

    header('Cache-Control: public');
    header('Content-Description: File Transfer');
    header('Content-Type: ' . $mimeType);
    header('Content-Type:application/octet-stream');
    header('Content-length: ' . $size);
    header('Content-Transfer-Encoding: binary'); 
    header('Content-Disposition:attachment;filename="' . $fileName . '"');
    readfile($url);
    

    Note here that I have changed all your PHP string definitions to use single quotes to present all string definitions consistently.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog