Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

I am accessing images from another website. I am getting this Error:

"failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request " error when copying 'some(not all)' images. here is my code.

$img=$_GET['img']; //another website url
$file=$img;

function getFileextension($file) {
       return end(explode(".", $file));
}
$fileext=getFileextension($file);
if($fileext=='jpg' || $fileext=='gif' || $fileext=='jpeg' || $fileext=='png' || $fileext=='x-png' || $fileext=='pjpeg'){
if($img!=''){
$rand_variable1=rand(10000,100000);
              $node_online_name1=$rand_variable1."image.".$fileext;

                $s=copy($img,"images/".$node_online_name1);

}

like image 545
krishna Avatar asked Dec 27 '10 14:12

krishna


3 Answers

The only issue I can think of is spaces being in the url, most likely in the file name. All spaces in a url need to be converted to their proper encoding, which is %20.

If you have a file name like this:

"http://www.somewhere.com/images/img 1.jpg"

You would get the above error, but with this:

"http://www.somewhere.com/images/img%201.jpg"

You should have to problems.

Just use the str_replace() to replace the spaces (" ") for their proper encoding ("%20")

It looks like this:

$url = str_replace(" ", "%20", $url);

For more information on the str_replace() check out The PHP Manual.

like image 111
Cello_Guy Avatar answered Oct 21 '22 06:10

Cello_Guy


I think preg_replace make more better sense as it will work with latest versions of the PHP as ereg_replace didn't worked for me being deprecated

$url = preg_replace("/ /", "%20", $url);
like image 29
Ravish Kumar Avatar answered Oct 21 '22 05:10

Ravish Kumar


I had the same problem, but it was solve by

$url = str_replace(" ", "%20", $url);

Thanks Cello_Guy for the post.

like image 42
npcoda Avatar answered Oct 21 '22 04:10

npcoda