2
2
3
3
function readFilesAndCreateJSON ($ directory ) {
4
4
$ files = scandir ($ directory );
5
+
6
+ // Read existing data from output.json if it exists
7
+ $ outputFile = 'output.json ' ;
5
8
$ jsonData = [];
6
9
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
+
7
17
foreach ($ files as $ file ) {
8
18
if ($ file != '. ' && $ file != '.. ' ) {
9
19
$ filePath = $ directory . '/ ' . $ file ;
@@ -15,19 +25,22 @@ function readFilesAndCreateJSON($directory) {
15
25
// Create tags array
16
26
$ tags = explode ('- ' , $ fileName );
17
27
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
+ }
25
38
}
26
39
}
27
40
28
41
// 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 ));
30
43
}
31
44
32
45
// Replace 'your_directory_path' with the actual directory path
33
- readFilesAndCreateJSON ('reports ' );
46
+ readFilesAndCreateJSON ('reports ' );
0 commit comments