的file_get_contents虚假URL时有空格(编码一切不工作)

问题描述:

所以,问题是在这条线的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

是的,它的确如此。当我编码时,一切都会返回错误。 –

使用此功能

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

它工作正常,谢谢 –

你得到假的,因为与状态301(永久移动)您重定向您的网址回复。 无论如何,如果您在访问此URL时仍想获取某些内容,请使用流上下文来遵循重定向。

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

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