2016-07-28 5 views
2

그래서, 문제는 그것이 작동하지 않는 공백 문자가 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

예. 그리고 인코딩 할 때, 모든 것은 거짓을 반환합니다. –

답변

6

이 기능을 사용하면 false를 받고있어

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 응답. 이 URL에 액세스 할 때 여전히 무언가를 얻으려면 스트림 컨텍스트를 사용하여 리디렉션을 따르십시오.

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

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