Skip to content

Commit dc57dab

Browse files
Update indexer.php
check if file exist, do not add again
1 parent ea0dba6 commit dc57dab

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Diff for: indexer.php

+22-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
function readFilesAndCreateJSON($directory) {
44
$files = scandir($directory);
5+
6+
// Read existing data from output.json if it exists
7+
$outputFile = 'output.json';
58
$jsonData = [];
69

10+
if (file_exists($outputFile)) {
11+
$jsonData = json_decode(file_get_contents($outputFile), true);
12+
}
13+
14+
// Create an associative array for quick lookup by 'url'
15+
$existingUrls = array_column($jsonData, null, 'url');
16+
717
foreach ($files as $file) {
818
if ($file != '.' && $file != '..') {
919
$filePath = $directory . '/' . $file;
@@ -15,19 +25,22 @@ function readFilesAndCreateJSON($directory) {
1525
// Create tags array
1626
$tags = explode('-', $fileName);
1727

18-
// Add file data to JSON array
19-
$jsonData[] = [
20-
'title' => $fileName,
21-
'url' => $file,
22-
'year' => $year,
23-
'tags' => $tags
24-
];
28+
// Check if the 'url' already exists in the existing data
29+
if (!isset($existingUrls[$file])) {
30+
// Add file data to JSON array only if the 'url' doesn't exist
31+
$jsonData[] = [
32+
'title' => $fileName,
33+
'url' => $file,
34+
'year' => $year,
35+
'tags' => $tags
36+
];
37+
}
2538
}
2639
}
2740

2841
// Write JSON data to file
29-
file_put_contents('output.json', json_encode($jsonData, JSON_PRETTY_PRINT));
42+
file_put_contents($outputFile, json_encode($jsonData, JSON_PRETTY_PRINT));
3043
}
3144

3245
// Replace 'your_directory_path' with the actual directory path
33-
readFilesAndCreateJSON('reports');
46+
readFilesAndCreateJSON('reports');

0 commit comments

Comments
 (0)