使用空格时PHP复制命令失败?

使用空格时PHP复制命令失败?,php,copy,Php,Copy,我正在执行以下命令: <?php copy ("http://localhost/.../DSCF8253.JPG" , "sites/default/files/DSCF8253.JPG"); // Success! copy ("http://localhost/.../DSCF8260.JPG" , "sites/default/files/DSCF8260.JPG"); // Success! copy ("http://localhost/.../HERMAN 085.jpg"

我正在执行以下命令:

<?php
copy ("http://localhost/.../DSCF8253.JPG" , "sites/default/files/DSCF8253.JPG"); // Success!
copy ("http://localhost/.../DSCF8260.JPG" , "sites/default/files/DSCF8260.JPG"); // Success!
copy ("http://localhost/.../HERMAN 085.jpg" , "sites/default/files/HERMAN 085.jpg" ); // Fail!
?>

前两份可以,但最后一份不行。为什么?

它必须与文件名有关(最后一个文件名在085之前有一个空格)

任何帮助都将不胜感激

http://localhost/.../HERMAN 085.jpg
应该是

http://localhost/.../HERMAN%20085.jpg

Copy&当涉及到无效的url时,http包装器比浏览器/用户代理更不宽容。url中的空格无效,因此应该是“d”。

最奇怪的事情是:使用%20的方法似乎不起作用,但经过一些尝试之后 徒劳(先用%20,然后引用文件名,然后双引号,然后 保护空间,如果我遗漏了什么,请告诉我),现在原始版本可以完美地工作。这是Windows10,PHP5.5.12,我们在2016年。 祝所有这些确定性有限状态系统好运:)

一个可能的解决方案顺便说一句,就是使用exec()并在操作系统上进行复制
系统级。再说一遍,这是操作系统特有的。

什么是
错误报告(E\u ALL)
say?你是否尝试过用URL中的
%20
替换空间来逃逸空间?源目录是否相同?此外,你知道,在localhost上,你可以使用文件路径,而不是使用慢速http传输(这可能也适用于空间)?@Pekka:我实际上是在从一个网站复制到另一个网站。我希望我可以使用文件路径来代替,但我不能为我正在做的事情。仍然不起作用,但你肯定在正确的道路上。我使用urlencode,但运气不好。还有其他建议吗?如果不行,我会检查Web服务器的访问日志,检查请求是否正常并返回数据。如果仍然失败,我会研究像
cURL
&
httprequest
这样的包,看看它们是否能够管理。cURL最终是最好的解决方案。有点像。如果对整个URL进行完全URL编码,它也会失败。从测试中我可以看出,空格是少数几个不能正确解释的字符之一。贫民区,但我建议用stru替换将空格改为%20。请查看
rawurlencode
它解决了我在这种情况下的问题。
//i used this code once i tried to copy images to a wordpress site and set post featured image
//i only mentioned the part you want and did not mention other parts
$image_url = 'http://example.com/images/my image with spaces.jpg';
try {
    //throw exception if can't move the file

    if (!copy(str_replace(" ","%20",$image_url),$file)) {
        $errors = error_get_last();
        throw new Exception('Could not copy file');
    }   

} catch (Exception $e) {
    echo $e->getMessage();
}
 //using urlencode will corrupt the url if used with the full url
 //it will generate something like http%dsf%swdfablablabla
 //if you need to encode you will encode anything after http://yoursite.com/{encode only works here}