Question

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);
?>
Was it helpful?

Solution

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));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top