Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-从外部服务器下载文件,文件名有引号'&引用;:';filename.zip';_Php_Curl_Download_Http Headers - Fatal编程技术网

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

PHP-从外部服务器下载文件,文件名有引号'&引用;:';filename.zip';,php,curl,download,http-headers,Php,Curl,Download,Http Headers,我制作了一个脚本,根据请求,服务器将返回一个特定的文件以从外部源下载,我使用外部源,因为我在另一台服务器上有无限带宽: $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($c

我制作了一个脚本,根据请求,服务器将返回一个特定的文件以从外部源下载,我使用外部源,因为我在另一台服务器上有无限带宽:

$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来自前端的请求。这是一个来自表单的简单帖子

一切都很好,但有些用户(不是所有用户)报告说他们无法打开该文件,因为文件名中有引号:
'filename.zip'
,而不是
filename.zip


我撞到了墙,我甚至不知道从哪里开始寻找,很明显,这种情况发生在一些mac用户身上。有什么想法吗?

HTTP标准要求您在
内容配置
标题中的filename参数周围加上双引号。在代码中可能是这样的:

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

请注意,我已将所有PHP字符串定义更改为使用单引号一致地表示所有字符串定义。

HTTP标准将要求您在
内容处置
标题中的filename参数周围加上双引号。在代码中可能是这样的:

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

请注意,我已将所有PHP字符串定义更改为使用单引号来一致地表示所有字符串定义。

这可能是因为在最后一行的第二行加了引号<代码>:我我认为这些是需要的。我们都对了一半。看看迈克的答案。是的,我在回答之前正在测试。谢谢那是因为你在最后一行的第二行加了引号<代码>:我我认为这些是需要的。我们都对了一半。看看迈克的答案。是的,我在回答之前正在测试。谢谢嗨,迈克,我正在做一些研究,我想你是对的,非常感谢,但是!我看到其他帖子在末尾加了一个分号:“.$filename.”这是要求吗?@multimediaxp不是要求,但可以添加。分号是参数分隔符。嗨,迈克,我正在做一些研究,我想你是对的,非常感谢,但是!我看到其他帖子在末尾加了一个分号:“.$filename.”这是要求吗?@multimediaxp不是要求,但可以添加。分号是参数分隔符。