0

I wrote this code that reads a csv file and compares it with photos in a folder and shows me the results of its comparison like this :

A1101_90: Several images
A1119_90: No image in the folder
A1119_854: No image in the folder
A1119_4023: No image in the folder
....

I would like to put them in a CSV file to have on two different columns:

A1101_90 | Several images
A1119_90 |No image in the folder
A1119_854 |No image in the folder
A1119_4023 | No image in the folder
....

I tried with a foreach with fputcsv but it only shows me the last item on my list, how can I change that?

<?php
echo '<pre>';
$dataImage = [];
$dataImageTmp = [];
$path = $_POST['path'];

$photos = scandir($path);
$photos = array_map('strtoupper', $photos);

if (($handle = fopen("../miniproducthub.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, 9000000, ";")) !== FALSE){
        if ($data[0] != null) {
            for ($i = 1; file_exists($fileName = $path.trim($data[6]).'_'.str_pad(trim($data[7]),4, "0", STR_PAD_LEFT).'-'.$i.'.JPG'); ++$i) {
                if (!in_array($fileName, $dataImage)){
                    $dataImage[$data[6] . '_' . $data[7]]['file'][$i] = $fileName;
                    $fileName = str_replace($path, '', $fileName);
                    if (!in_array($fileName, $dataImageTmp)){
                        $dataImageTmp[] = $fileName;
                    }
                }
                if (isset($dataImage[$data[6] . '_' . $data[7]]['TOTAL'])) {
                    $dataImage[$data[6] . '_' . $data[7]]['TOTAL']++;
                } else {
                    $dataImage[$data[6] . '_' . $data[7]]['TOTAL'] = 1;
                }
            }
              if ($i == 1) 
              { 
                    $resultat = [$data[6] . '_' . $data[7].' : No image in the folder'];
                    print_r($data[6] . '_' . $data[7].' : No image in the folder'."\n");
            }
            elseif ($i == 2)
                {
                    $resultat = [$data[6] . '_' . $data[7].' : 1 single view of the image'];
                     print_r($data[6] . '_' . $data[7].' : 1 single view of the image'."\n");
                }
                else
                {
                    $resultat = [$data[6] . '_' . $data[7].' : Several images'];
                    print_r($data[6] . '_' . $data[7].' : Several images'."\n");
                }   
       }

       fputcsv($fichier_csv, $resultat, $delimiteur);
    }
  
    fclose($fichier_csv);//close
}
?>
 <!-- Link file -->
<a href='csv/photos.csv' target='_blank'>Download</a>

1 Answer 1

1

When your first loop runs, it always overwrites the data using $resultat = , a simple solution would be to change this to add the items using []...

$resultat[] = [$data[6] . '_' . $data[7]];

also your output should be

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

so $result and not $resultat, also without the <br>;

But personally I would write the data inside the first loop.

So after the fgets()...

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));

and before the end of the loop (inside the close })...

fputcsv($fichier_csv, $resultat, $delimiteur);
Sign up to request clarification or add additional context in comments.

5 Comments

Inside which loop I put fputcsv($csv_file, $result, $limiter) ? @Nigel Ren
Looks as though it should be inside the while loop.
I updated my code with your indications but in my csv I have everything in one column, how to separate the values of the text in two columns? @Nigel Ren
I think you need to separate out your data, an example of which is $resultat = [$data[6] . '_' . $data[7].' : Several images'];, the last part I think should be separate - $resultat = [$data[6] . '_' . $data[7], 'Several images'];. I've changed the bit in the middle of [7], 'Several
Thank you very much, I've changed and it works perfectly! @Nigel Ren

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.