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

Community
  • 1
  • 1
Mh Salim
  • 1
  • 1
  • 3

4 Answers4

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

Hamed Al-Khabaz
  • 680
  • 4
  • 7
  • Don't use the '@' operator in order to hide errors! It is *very* slow. – ComFreek Jan 03 '13 at 16:33
  • 1
    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 Jan 03 '13 at 16:42
  • @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`. – Hamed Al-Khabaz Jan 03 '13 at 16:49
  • @MhSalim are you sure it's not your link that's invalid ? What's the output anyways ? – Hamed Al-Khabaz Jan 03 '13 at 17:12
  • 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) – Mh Salim Jan 03 '13 at 17:34
  • @MhSalim It is possible that the server (where the image is hosted) rejects any requests without appropriate (=from real browsers) header tags. You could emulate this behaviour with cURL but before doing so, I would verify whether this is really the problem. – ComFreek Jan 03 '13 at 21:11
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));
Sorin Trimbitas
  • 1,467
  • 18
  • 35
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.

MrCode
  • 63,975
  • 10
  • 90
  • 112
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 );

?>
alizahid
  • 959
  • 6
  • 17