PHP Download images from hyperlinks from csv file to a specific folder

Hello! I am trying to save some images from hyperlinks to a folder. However I am getting this error: file_put_contents(./images-folder/): failed to open stream: No such file or directory. Here is my code:

<?php
$in_file = 'gabriel-images-urls.csv';
$destdir = './images-folder/';
while ($data = fgetcsv($fd)) {
if(!empty($data)){

$filename = $data[2];
// echo $filename. "\n";
$img=file_get_contents_curl($filename);
/*if (!is_dir('images-folder/' . $filename)) {
// dir doesn't exist, make it
mkdir('images-folder/' . $filename);
}
*/
file_put_contents('./images-folder/', $img);
//$image = "/uploads/" . $filename;
//echo $image . "\n";
}
}

function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of 
printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

I think my $desdir variable is wrong and this is the reason the images are not put in the folder

file_put_contents needs a file, to put the contents, you are just giving a directory.

I know but how to save to a folder, because the images are 3000+

you need to give the file a name

you have to save each file under a specific name. Read from filename => save to filename.

Sponsor our Newsletter | Privacy Policy | Terms of Service