Theo27
Theo27

Reputation: 405

How to compare an image folder with a csv file in php?

I have a folder of images that are named like this:

enter image description here

I have a csv file with this information and it is the two columns 'Product Style' and 'Product Color ID' that I am interested in:

Season Code ID;Product Business Area DESC;Product Business Area ID;Product Group DESC;Product Group ID;Product Item Group DESC;Product Item Group ID;Product Gender;Product Activity;Product Style Code;Product Style Name;Product Colour DESC;Product Colour ID;Product Size ID;Sample size;All Sizes;Product Sku ID;Product Barcode ID;Country Of Origin;Construction;Weight;Supply Chain Code ID;Fob Unit Price;Fob Currency;Landed Unit Price;Landed Currency ;Wholesale UnitPrice;Wholesale Currency ;Factory Code;Season Type
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;10;10;6, 8, 10, 12, 14, 16, 18;13363/902/10;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;12;10;6, 8, 10, 12, 14, 16, 18;13363/902/12;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;14;10;6, 8, 10, 12, 14, 16, 18;13363/902/14;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;16;10;6, 8, 10, 12, 14, 16, 18;13363/902/16;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;18;10;6, 8, 10, 12, 14, 16, 18;13363/902/18;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer
FY22S1;Apparel;;Wovens;460;Shorts;4602;Unisex;City Life;13363;Praca Hiking Short v2 Wmn;Black;902;6;10;6, 8, 10, 12, 14, 16, 18;13363/902/6;9,42E+12;Made in Vietnam;"  Main: Nylon / Elastane 95/5 (%) ;  : Exclusive of Decoration  (%) ";175 g ;;;NZD;;;;;SPLR_1117;Summer

I wrote a php script that allows me to compare the name of the images with the one in the csv file to know which photo is missing in the folder :

13363_902;"Missing image"
13363_902;"Missing image"
13363_902;"Missing image"
13363_902;"Missing image"
13363_902;"Missing image"
13363_902;"Missing image"
13363_902;"Missing image"
13363_913;"Missing image"
13363_913;"Missing image"
13363_913;"Missing image"
13363_913;"Missing image"
13363_913;"Missing image"
13363_913;"Missing image"

The problem is that the script is very long for large folders of images and I noticed that some images in the folder were counted as missing when they are in the folder.

Where can this error and speed problem come from please?

<?php

// ini_set('max_execution_time', 61200);
$path = 'C:/wamp64/www/KMD/verif/SS22 International Range (2000x2000 jpeg)/';

   $dir  = $path;
   $allFiles = scandir($dir);

   foreach($allFiles as $file) 
   {
        if (!in_array($file,array(".","..")))
      { 

          $file = $dir.$file;
          $filename = basename( $file );

          //We delete Online version-
          $explode = explode("-", $filename);
          // echo $explode[1]; //14109_NOR_Tauro_Hooded_Jacket_Men_a.jpg

          $explode2 = explode("_", $explode[1]);
          $name_picture = $explode2[0].'_'.$explode2[1]; 
          // echo $name_picture;

          if (($handle = fopen("KMD.csv", "r")) !== FALSE) {
            fgets($handle); // skip header line
        
                // Create CSV output file
                $chemin = 'csv/photos.csv'; 
                $delimiteur = ';';
        
                $fichier_csv = fopen($chemin, 'w+');
                fprintf($fichier_csv, chr(0xEF) . chr(0xBB) . chr(0xBF));
        
                while (($data = fgetcsv($handle, 0000000, ";")) !== FALSE) {
                 if ($data[0] != null) {
                    
                    $name_file = $data[9].'_'.$data[12];
                    // echo $name_file;
                    // echo '<br>';
                    // echo $name_picture;
                    // echo '<br>';

                    if ($name_file != $name_picture)
                    {
                        $resultat = [$name_file,'Missing image'];
                        print_r($name_file.': Missing image');
                        echo "<br>";
                    }
                    else
                    {
                        $resultat = [$name_file,'Image in folder'];
                        print_r($name_file.': Image in folder');
                        echo "<br>";
                        
                    }
               }
        
               fputcsv($fichier_csv, $resultat, $delimiteur);
            }
          
            fclose($fichier_csv);//close
        
        }                    
     }
  }

?>

<!-- Link file -->
Result file :
<a href='csv/photos.csv' target='_blank'>Dowload</a>

Upvotes: 0

Views: 136

Answers (1)

Syscall
Syscall

Reputation: 19780

First, for better performance, you should avoid to open/write/close in a file in foreach loop.

// Create CSV output file
$chemin = 'csv/photos.csv'; 
$delimiteur = ';';
$fichier_csv = fopen($chemin, 'w+');
fprintf($fichier_csv, chr(0xEF) . chr(0xBB) . chr(0xBF));

foreach($allFiles as $file) 
{
    // do stuff then, write
    fputcsv($fichier_csv, $resultat, $delimiteur);
}

fclose($fichier_csv);

Then, in your loop you are comparing the current file to the whole list and add the resultat each time. That's why you don't have to expected results.

You could prepare a simple array of file.

$kmd = [];
if (($handle = fopen("KMD.csv", "r")) !== FALSE) {
    fgetcsv($handle, 0, ";"); // skip header line
    while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
        if ($data[0] != null) {
            $kmd[] = $data[9].'_'.$data[12];
        }
    }
    fclose($handle);
}

Then, in the loop :

foreach($allFiles as $file) 
{
    // ...

    if (! in_array($name_picture, $kmd))
    {
        $resultat = [$name_picture,'Missing image'];
        print_r($name_file.': Missing image');
        echo "<br>";
    }
    else
    {
        $resultat = [$name_picture,'Image in folder'];
        print_r($name_file.': Image in folder');
        echo "<br>";
        
    }

    fputcsv($fichier_csv, $resultat, $delimiteur);

Upvotes: 1

Related Questions