我认为这会为你工作:
function file_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts))
;
}
echo file_url("http://example.com/foo/bar bof/some file.jpg") . "\n";
echo file_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n";
echo file_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n";
输出
http://example.com/foo/bar%20bof/some%20file.jpg
http://example.com/foo/bar%2Bbof/some%2Bfile.jpg
http://example.com/foo/bar%20bof/some%20file.jpg
备注:
我可能会使用urldecode
和urlencode
作为输出将是相同的每个网址。 rawurlencode
将保留+
,即使%20
可能适用于您正在使用的任何网址。
难道你不是指['file_get_contents'](http://php.net/file_get_contents)? –
'some + name.jpg'应该可以工作。它如何下载失败? 404错误? 400个不好的要求? file_put_contents()用于保存数据。如果你提供了一个URL,你就会有效地尝试进行http上传,这很可能不是你想要的。 –
他遇到的问题是'rawurlencode'将会对字符串进行双重编码,如果它已经被编码的话。然而,在对它们进行编码之前简单解码所有字符串看起来并不会起作用。他需要一个适用于所有URL的函数 –