我認爲這會爲你工作:
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的函數 –