1
How are you all? This is my first post because I am a beginner with PHP and CURL and I needed to create a script to download several images in repositories through a file .csv. It reads the url where the file is, and saves in a pre-established directory, I use the "basename" function to recover the name of the image that is in the URL. Until then the script does everything smoothly saves the right images everything correctly, but I can’t change this output to rename the files as needed. I wonder if I can use the same. csv add one more column, or insert in the script the specific names for each image, that is, I already know what this name would be and the order to rename would be the one in the . CSV, look at the script:
<?php
$csvFile = file('imagensuno.csv'); // Aqui ele pega o arquivo .csv com as URL das imagens
foreach ($csvFile as $line) {
$url = str_getcsv($line);
$ch = curl_init($url[0]);
$name = basename($url[0]); //Aqui ele usa o nome da imagens na URL para salvar ela com o nome que ela tem na URL
if (!file_exists('G:\cca_imagens/' . $name)) {
$fp = fopen('G:\cca_imagens/' . $name, 'wb');
}
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
//echo str_getcsv($line);
}
the function str_getcsv
, I will scroll down the line of the csv file "imagensuno.csv" which is located in the same directory as the script.
to file_exists
which will check whether the image has been saved before, very useful if script execution is stopped.
Also note the use of the function basename
to recover the name of the image that is in the URL.
And this is where my doubt is, and I can’t remember if I can or can make you save the name I need that would be:
Ex of file . csv
Coluna 1 Coluna 2
https://enderecodaimagens.com.br/img/foto.png, foto.png => sku435.png,
https://enderecodaimagens.com.br/img/foto2.png, foto2.png => sku444.png,
https://enderecodaimagens.com.br/img/foto3.png foto.png => sku865.png
How would they all be in the same. csv I want to rename these not by URL name but by Column 2 action, but I don’t know if it’s possible, I can’t think of how to do this output.
People need this Help and if someone wants to use the script this the layout and logico want to improve it also think very good ok?
abs
The file 'imagensuno.csv' is the file that holds the two columns, and you want to select the image related to column 1 and rename with column name 2, that’s it ?
– AnthraxisBR
Yes, or if I can do this another way. I think of . csv so that everything stays in one run and file, because on average this will occur with on average 25000 images
– Anderson Leão Gimenes