如果在URL中包含空格(%20),为什么PHP强制下载脚本不起作用?
我使用的PHP强制下载脚本如下:-如果在URL中包含空格(%20),为什么PHP强制下载脚本不起作用?,php,readfile,Php,Readfile,我使用的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: applicat
$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;
如果我链接的URL如下:-/image.php?name=Germany.png&file=https%3A%2F%2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3DGermany%26zoom%3D15%26size%3D240x320%26maptype%3Droadmap%26mobile%3Dtrue%26sensor%3Dfalse,那么它可以正常工作了
如果我在URL中包含一个空格(%20)并尝试访问它,浏览器会显示“下载失败”
示例URL:-/image.php?name=image.png&file=https%3A%2F%2Fmaps.google.com%2Fmaps%2Fapi%2Fstaticmap%3Fcenter%3DRiver%20anna%26缩放%3D15%26大小%3D240x320%26地图类型%3dromap%26移动%3Dtrue%26传感器%3dflse
那么,为什么会发生这种情况?怎么了?用
-
替换空格,然后尝试读取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文档,“superglobals$\u GET和$\u REQUEST已经被解码了”,所以%20被空格代替。 以下代码应该可以工作:
readfile(urlencode($file_Url));
+1对于代码的一般不安全性虽然被问到的实际问题对我来说是个谜,但我想指出,这个脚本非常危险。如果有人在文件中键入“config.php”,而您有这样的配置文件。它会显示你所有的机密信息。您应该在使用输入之前对其进行过滤<代码>过滤输入(输入获取“文件”,过滤验证URL)是
退出代码>不必要?你没有把它包括在内!我使用了echo readfile(str_replace(“,“%20”,“$file_Url”);退出代码>取而代之!但这两种方法都有效!这还将对/
和其他不需要的内容进行编码。使用OP示例尝试您的解决方案。它将以errorOk结束,因此需要做更多的工作,首先用“/”分割路径,然后对除域之外的所有内容进行编码,然后再次加入