2016-07-28 57 views
2

所以,問題是在這條線的file_get_contents虛假URL時有空格(編碼一切不工作)

$imageString = file_get_contents($image_url); 

與有空格字符它不工作的網址。但如果我製作

$imageString = file_get_contents(urlencode($image_url)); 

沒有什麼作品。我一直在變量中接收false。

的ULR是什麼樣的:

https://s3-eu-central-1.amazonaws.com/images/12/Screenshot from 2016-04-28 18 15:54:20.png 
+0

永遠不要在文件名中首先使用空格!!!只有'Windows'默認有時會這樣做。 – JustOnUnderMillions

+0

因此,它在圖像文件名沒有空格時有效? – apokryfos

+0

是的,它的確如此。當我編碼時,一切都會返回錯誤。 –

回答

6

使用此功能

function escapefile_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 escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "\n"; 
echo escapefile_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n"; 
echo escapefile_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n"; 
+0

這工作正常! 謝謝 –

+0

它工作正常,謝謝 –

0

你得到假的,因爲與狀態301(永久移動)您重定向您的網址回覆。 無論如何,如果您在訪問此URL時仍想獲取某些內容,請使用流上下文來遵循重定向。

$context = stream_context_create(
    array(
     'http' => array(
      'follow_location' => false 
     ) 
    ) 
); 

$html = file_get_contents('http://www.example.com/', false, $context);