How to rename files in a .CSV list in your output with PHP and CURL?

1

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

    
asked by anonymous 24.02.2018 / 16:55

2 answers

0
Rename file: '. $ Rename.' -
';   // Tests whether the file exists   if (! file_exists ('C: \ wamp \ www \ projects \ dw photo cca /'. $ name)) {     // Test to rename the file     if (rename ("C: \ wamp \ www \ projects \ dw photo cca \ $ name", "C: \ wamp \ www \        echo 'Original file:'. $ name. '
Renamed file:'. $ rename. '
Status: Successfully renamed, line'. $ count. '' ';     } else {        echo 'Original file:'. $ name. '
Renamed file:'. $ rename. '' Status: Error when renaming, line '. $ count.' '';     }   }   $ count + = 1; } ? >

This above the edited code!

    
23.03.2018 / 23:51
0

Assuming you have .csv in the following format:

http://127.0.0.1/stack/fopencsv/imagem1.jpg, novaimagem1.jpg
http://127.0.0.1/stack/fopencsv/imagem2.jpg, novaimagem2.jpg
http://127.0.0.1/stack/fopencsv/imagem3.jpg, novaimagem3.jpg
http://127.0.0.1/stack/fopencsv/imagem4.jpg, novaimagem4.jpg
http://127.0.0.1/stack/fopencsv/imagem5.jpg, novaimagem5.jpg

You can rename using the php rename function, and fgescsv with the following script:

<?php

//Abrindo o manipulador
$handle = fopen('csv.csv', 'r');

$count = 1;
$file = fgetcsv($handle);
//Abrindo o arquivo e lendo as linhas
while ($row = fgetcsv($handle)) {
    //removendo a url (url não é aceita no 'rename')
    $name = str_replace('http://127.0.0.1/stack/fopencsv/img/','',$row[0]);
    $rename = $row[1];

    echo 'Arquivo original: ' . $name . '<br>Arquivo renomeado: ' . $rename . '<br>-<br>'.PHP_EOL;
    //Testa se o arquivo existe
    //use __dir__ , ou outra coisa, o importante é apontar o local correto do arquivo, dentro do mesmo servidor.
    if (!file_exists(__DIR__ . '\' . $name)) {
        //Teste para renomear o arquivo
        if (rename(__DIR__ . '\img\' . $name, __DIR__ . '\img\' . $rename)) {
            echo 'Arquivo original: ' . $name . '<br>Arquivo renomeado: ' . $rename . '<br>Status: Renomeado com sucesso, linha ' . $count . '<br>';
        } else {
            echo 'Arquivo original: ' . $name . '<br>Arquivo renomeado: ' . $rename . '<br>Status: Erro ao renomear, linha ' . $count . '<br>';
        }
    }
    $count += 1;
}

Rules:

1 - The files must be on the same server where the script runs.

2 - Replace the url where the file is located by the local path.

3 - My files are in the folder: Script: c: \ xampp \ htdocs \ stack \ fgetscsv \ Images: c: \ xampp \ htdocs \ stack \ fgetscsv \ img

4 - Give the file permission (probably already ok)

    
24.02.2018 / 22:42