PHP - 从外部服务器下载文件,文件名有引号 " ' " : 'filename.zip'
PHP - Download file from external server, filename has quotes " ' " : 'filename.zip'
我制作了一个脚本,根据请求,服务器会 return 从外部源下载特定文件,我使用外部源是因为我在另一台服务器上有无限带宽:
$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来自于前端的请求。这是一个简单的 post 表格。
一切正常,但一些用户(不是全部)报告说他们无法打开文件,因为文件名有引号:'filename.zip'
,而不是 filename.zip
我碰壁了,我什至不知道从哪里开始寻找,显然这种情况发生在一些 mac 用户身上。有什么想法吗?
HTTP 标准要求您在 Content-Disposition
header 中的文件名参数两边加上双引号。在您的代码中可能看起来像这样:
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 字符串定义更改为使用单引号以一致地呈现所有字符串定义。
我制作了一个脚本,根据请求,服务器会 return 从外部源下载特定文件,我使用外部源是因为我在另一台服务器上有无限带宽:
$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来自于前端的请求。这是一个简单的 post 表格。
一切正常,但一些用户(不是全部)报告说他们无法打开文件,因为文件名有引号:'filename.zip'
,而不是 filename.zip
我碰壁了,我什至不知道从哪里开始寻找,显然这种情况发生在一些 mac 用户身上。有什么想法吗?
HTTP 标准要求您在 Content-Disposition
header 中的文件名参数两边加上双引号。在您的代码中可能看起来像这样:
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 字符串定义更改为使用单引号以一致地呈现所有字符串定义。