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

6

11

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);

}

Spinner answered 27/12, 2010 at 14:10 Comment(2)
Is this all of your code to reproduce the problem? There's an unclosed curly brace at the end. Also, where are you actually sending a request to a web server? When you get the error, what is the request?Counterfactual
i am copying images from another site. when ever copy function executes then i am getting this error. $s=copy($img,"images/".$node_online_name1);Spinner
R
21

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);
Renatorenaud answered 4/2, 2013 at 19:56 Comment(0)
H
12

I had the same problem, but it was solve by

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

Thanks Cello_Guy for the post.

Hepcat answered 18/5, 2012 at 5:53 Comment(0)
G
8

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.

Gemma answered 6/6, 2011 at 23:16 Comment(1)
Better than str_replace would be calling urlencode on the "img 1" stringTrooper
D
1

Use the function rawurlencode()

Encodes the given string according to » RFC 3986.

http://php.net/manual/ru/function.rawurlencode.php

Dehumidify answered 2/12, 2013 at 15:44 Comment(0)
K
1

Even a trailing blank in the url can cause php file($url) to fail. In recent versions of php or apache even a trailing blank in the url will cause the error. So the url appears to work in a browser because the browser knows enough to %20 the trailing blank or ignore it. That was my error anyway.

Older LAMP allowed it. (ie. same code ran ok). Easy fix.

Kovno answered 11/4, 2018 at 16:56 Comment(0)
F
0

It seems that the URL has some spaces or other special characters, you need to encode it, use urlencode() function

Force answered 13/4, 2020 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.