2

Sorry guys if its a duplicate, I coudnt find similar topic. So, lets say there is a URL with image content, called "www.test.com/test.jpg". I want to save the image to a folder using php, so I use

  file_put_contents('test.jpg', 'www.test.com/test.jpg');

and it is working fine, saving the image to the current folder. However, if the image URL looks like this:

  www.test.com/test.jpg?id={1a2b3c-4d5e6f}

I cannot use file_put_contents to save the image. If I try to remove the ?id={1a2b3c-4d5e6f} part, the browser wont open the image. Any ideas how to make the save successful?

7
  • What is the code you use to generate the image? Commented Feb 26, 2016 at 9:14
  • What do you get when you try to open the image? A 404? Or a broken image? Commented Feb 26, 2016 at 9:16
  • I get this -> HTTP status 400 - type Status report - The request sent by the client was syntactically incorrect (). Commented Feb 26, 2016 at 9:17
  • Even without the query string? The query string is in fact syntactically incorrect, you have to urlencode the curly braces. Commented Feb 26, 2016 at 9:18
  • Are you sure that the image returned is not really corrupted? Commented Feb 26, 2016 at 9:24

3 Answers 3

1

Don't use the URI as filename. Potential issues with this go beyond what you have already found.

Instead, something like the following will work.

$uri = 'http://www.test.com/test.jpg?id={1a2b3c-4d5e6f}';
$data = file_get_contents($uri);
file_put_contents(md5($uri),$data);

If the original URI for the image is needed, store it in a DB or text file with a reference back to the MD5. Something like this might work (without testing).

$uri = 'http://www.test.com/test.jpg?id={1a2b3c-4d5e6f}';
$uriMd5 = md5($uri);
$data = file_get_contents($uri);
file_put_contents($uriMd5,$data);
file_put_contents($uriMd5.'.txt',$uri);
Sign up to request clarification or add additional context in comments.

Comments

0
$variable="www.test.com/test.jpg?id={1a2b3c-4d5e6f}";
$variable=substr($variable, 0, strpos($variable, "?")); // www.test.com/test.jpg
file_put_contents('test.jpg', $variable);

Try this.

1 Comment

After removing the ?id= part, the URL to the image is broken.
0

The solution: First, the function to get data:

    function getData($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 

        $data = curl_exec( $ch ); 
        curl_close($ch);  

        if ($data) return $data; 
        else die("no data - ".$url);
    }

then use:

 file_put_contents('test.jpg', getData(..URL..));

1 Comment

I'd call it rather workarounded than solved. But storing data you retrieve from the web in folders with public access is risky: If someone masks the URL or hijacks the connection, they can place malicious code on your system and execute it. So make sure to store the images in a folder where no scripts are executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.