0

First of all I have tried combination of file_put_contens with fopen, fopen with frwrite without any sucess. I have now found this where Im a step further, but not at the finish line. Saving image from PHP URL

I have a url - domain.tld/images/ and the structure is 1.jpg, 2.jpg and so on. With this I can get the last pictures saved 15.jpg, but I dont seem to the the other pictures. Not sure if I should use curl_multi, or putting the curl in a loop or whats the best way to do this?

<?php
$base_url = "domain.tld/images/";
$sufix = ".jpg";
$id_start= "1";
$id_end = "15";
$path_to_save = "files/";

foreach(range($id_start,$id_end) as $id)

$ch = curl_init();
$fp = fopen('filer/'.$id.$sufix, 'wb');
curl_setopt($ch, CURLOPT_URL, $base_url.$id.$sufix);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Community
  • 1
  • 1
fixern256
  • 23
  • 5

1 Answers1

0

You didn't state what the problem was with file_get_contents(), so if your host is configured to allow it (allow_url_fopen = on in php.ini), this should work:

$base_url = "http://domain.tld/images/";

foreach(range($id_start, $id_end) as $id) {
    file_put_contents($path_to_save.$id.$sufix, file_get_contents($base_url.$id.$sufix));
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • I tried your solution and then I get PHP Warning: file_put_contents() expects at least 2 parameters, (as it should) so then I tried foreach(range($id_start,$id_end) as $id) file_put_contents($path_to_save.$id.$sufix,fopen($base_url.$id.$sufix, 'r') ); This creates all the files but the files are size = 0 and actually not files, and gives error PHP Warning: fopen(domain.tld/images/1.jpg): failed to open stream: No such file or directory in /home/server/public_html/frimerker/jpg.php on line 13 and same error all to 15.jpg allow_url_fopen is on on server and local php.ini files is disabled. – fixern256 Nov 12 '13 at 16:59
  • `Warning: fopen(domain.tld/images/1.jpg): failed to open stream: No such file or directory in /home/server/public_html/frimerker/jpg.php` is your clue. I edited to add http:// – AbraCadaver Nov 12 '13 at 18:45
  • Edited. My bad for spelling "suffix" correctly the first time. :) – AbraCadaver Nov 13 '13 at 13:41