How are everyone? This is my first post because I'm a beginner with PHP and CURL and I needed to create a script that will download several images into repositories through a .csv file. It reads the url where the file is, and saves it to a pre-established directory, I use the "basename" function to retrieve the name of the image that is in the URL. Until then the script does everything without problems saves the correct images everything correctly, but I can not change this output to rename the files according to the need that I have. I would like to know if I can use the same .csv to add another column, or insert the specific names for each image in the script, that is, I already know what the name would be and the rename order would be the one in .CSV, 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'll go through the line of the csv file "imagesuno.csv" which is located in the same directory as the script.
a file_exists
that will check if the image has already been saved before, very useful if script execution is interrupted.
Also note the use of the basename
function to retrieve the name of the image that is in the URL.
And here's my question, and I can not remember if I can or can not save the name I need it to look like this:
Ex .csv file
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
I would like to rename these not by the name of the URL but by the action of Column 2, but I do not know if it's possible, I can not think how to do this output.
People need this Help and if someone wants to use the script is willing and logical to improve it also think very good ok?
abs