我编写了这段代码,它读取 csv 文件并将其与文件夹中的照片进行比较,并向我显示比较结果,如下所示:
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
....
我想将它们放入 CSV 文件中,以便放在两个不同的列上:
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
....
我尝试使用 foreach 和 fputcsv 但它只显示列表中的最后一项,我该如何更改它?
<?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");
}
喵喵时光机