如果我在 URL 中包含 space (%20),为什么 PHP 强制下载脚本不起作用?
Why PHP Force Download script does not work if I include a space (%20) inside the URL?
我正在使用 PHP 强制下载脚本,如下所示:-
$file_Name = $_GET['name'];
$file_Url = $_GET['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_Name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
readfile($file_Url);
exit;
如果我的 link 的 URL 是这样的:- /image.php?name=Germany.png&file=https%3A%2F%2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3D德国%26zoom%3D15%26size%3D240x320%26maptype%3Droadmap%26mobile% 3Dtrue%26sensor%3Dfalse,所以它工作没有任何问题!
如果我在 URL 中包含一个 space (%20) 并尝试访问它,浏览器会显示 "Download Failed"!
示例URL:- /image.php?name=Image.png&file=https%3A%2F% 2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3DRiver%20Annan%26zoom%3D15%26size%3D240x320%26maptype%3Droadmap%26mobile%3Dtrue%26sensor%3Dfalse
那么,为什么会这样?怎么了?
将space替换为-
然后尝试阅读url
$file_Name = $_GET['name'];
$file_Url = $_GET['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_Name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
echo readfile(str_replace(" ","-",$file_Url));
exit;
会发生这种情况,因为根据 php 文档,"The superglobals $_GET and $_REQUEST are already decoded.",所以 %20 被 space 替换了。
以下代码应该有效:
readfile(urlencode($file_Url));
+1 表示代码的一般不安全性
我正在使用 PHP 强制下载脚本,如下所示:-
$file_Name = $_GET['name'];
$file_Url = $_GET['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_Name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
readfile($file_Url);
exit;
如果我的 link 的 URL 是这样的:- /image.php?name=Germany.png&file=https%3A%2F%2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3D德国%26zoom%3D15%26size%3D240x320%26maptype%3Droadmap%26mobile% 3Dtrue%26sensor%3Dfalse,所以它工作没有任何问题!
如果我在 URL 中包含一个 space (%20) 并尝试访问它,浏览器会显示 "Download Failed"!
示例URL:- /image.php?name=Image.png&file=https%3A%2F% 2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3DRiver%20Annan%26zoom%3D15%26size%3D240x320%26maptype%3Droadmap%26mobile%3Dtrue%26sensor%3Dfalse
那么,为什么会这样?怎么了?
将space替换为-
然后尝试阅读url
$file_Name = $_GET['name'];
$file_Url = $_GET['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_Name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
echo readfile(str_replace(" ","-",$file_Url));
exit;
会发生这种情况,因为根据 php 文档,"The superglobals $_GET and $_REQUEST are already decoded.",所以 %20 被 space 替换了。 以下代码应该有效:
readfile(urlencode($file_Url));
+1 表示代码的一般不安全性