0

I have tried to download an image from a PHP link. When I try the link in a browser it downloads the image. I enabled curl and I set “allow_url_fopen” to true. I’ve used the methods discussed here Saving image from PHP URL but it didn’t work. I've tried "file_get_contents" too, but it didn't work. I made few changes, but still it doesn’t work. This is the code

$URL_path='http://…/index.php?r=Img/displaySavedImage&id=68';
$ch = curl_init ($URL_path);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
$fp = fopen($path_tosave.'temp_ticket.jpg','wb');
fwrite($fp, $raw);
fclose($fp);

Do you have any idea to make it works? Please help. Thanks

4
  • Is the semicolonn the error ? Commented Jan 3, 2013 at 16:29
  • What exactly is the error displayed? Commented Jan 3, 2013 at 16:38
  • There isn't any syntax error. Every time an empty image saves on the hard drive but when I try the link in the browser, it downloads the image. Commented Jan 3, 2013 at 17:22
  • look at my updated answer Commented Jan 4, 2013 at 9:49

4 Answers 4

2
<?php
    if( ini_get('allow_url_fopen') ) {
      //set the index url
      $source  = file_get_contents('http://…/index.php?r=Img/displaySavedImage&id=68');
      $filestr = "temp_ticket.jpg";
      $fp = fopen($filestr, 'wb');
      if ($fp !== false) {
        fwrite($fp, $source);
        fclose($fp);
      }
      else {
        // File could not be opened for writing
      }
    }
    else {
      // allow_url_fopen is disabled
      // See here for more information:
      // http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
    }
?>

This is what I used to save an image without an extension (dynamic image generated by server). Hope it works for you. Just make sure that the file path location is fully qualified and points to an image. As @ComFreek pointed out, you can use file_put_contents which is the equivalent to calling fopen(), fwrite() and fclose() successively to write data to a file. file_put_contents

Sign up to request clarification or add additional context in comments.

6 Comments

Don't use the '@' operator in order to hide errors! It is very slow.
I've improved the code a little bit. But why don't you use file_put_contents()? It's the same as calling fopen(), fwrite() and fclose().
@ComFreek good observation. I did not know of such a function as of today, so thanks. I'll leave it like that so the one asking the question see what's going on and let him have a piece of action using file_put_contents.
@MhSalim are you sure it's not your link that's invalid ? What's the output anyways ?
The link is valid, when I try that in a browser, I will have the image (700 KB). But when I try the code, I only have an empty image (1KB)
|
2

You can use it as a function :

function getFile($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $tmp = curl_exec($ch);
    curl_close($ch);
    if ($tmp != false){
        return $tmp;
    }
}

And to call it :

$content = getFile(URL);

Or save its content to a file :

file_put_contents(PATH, getFile(URL));

3 Comments

Try now, I have added CURLOPT_FOLLOWLOCATION .. maybe your URL triggers a redirect too.
Thanks, but it doesn't work, I guess it should be some thing about server side, instead of that I open the blob from the database to write it to a temp file.
Is that URL on your own server?
0

You're missing a closing quote and semicolon on the first line:

$URL_path='http://…/index.php?r=Img/displaySavedImage&id=68';

Also, your URL is in $URL_path but you initialise cURL with $path_img which is undefined based on the code in the question.

Comments

0

Why use cURL when file_get_contents() does the job?

<?php

    $img = 'http://…/index.php?r=Img/displaySavedImage&id=68';

    $data = file_get_contents( $img );

    file_put_contents( 'img.jpg', $data );

?>

4 Comments

Because cURL is a hell of a lot faster
file_get_contests may be disabled for external use on some servers
@SorinTrimbitas He did say he's set allow_url_fopen to true, so it couldn't be that.
Usually a general solution is preferred to one that works only on some parts.

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.