Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My ultimate goal is to get files/folders as json like this:

{
    "file1": "file1 contents",
    "file2": "file2 contents",
    "ThisIsAFolder": {
        "fileInsideFolder": "folder contents"
        "fileInsideFolder2": "folder contents"
    }
}


What I have tried:

PHP
<?php
//this file is for aOS

//this gets your files in the directory (Insert starting directory here)
$main = "message_standalone";

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($main));
$allFiles = array_filter(iterator_to_array($iterator), function($file) {
    return $file->isFile();
});

$filenames = array_keys($allFiles);

//print_r($filenames);

//$filearray=array();

foreach($filenames as $filename){

$filenamens=trim(substr($filename, strpos($filename, '\\') + 1));

$filearray[$filenamens]=htmlentities(file_get_contents($filename));

}

$json=json_encode($filearray, JSON_PRETTY_PRINT);

echo "<pre>";
print_r($json);
echo "<//pre>";

?>
Posted
Updated 9-Feb-19 1:36am
Comments
Richard MacCutchan 9-Feb-19 5:35am    
So what is the question?

1 solution

The following line should be removed, as it adds the content of each file to the output:
PHP
$filearray[$filenamens]=htmlentities(file_get_contents($filename));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900