1

I'm trying to get all directory and sub directory list as a JSON object but I'm not getting any ideas on how to do that.

currently I'm reading like this

->test.php 

$path = realpath('Question Papers');
$self_files = glob($path . '/*');
$recursive_files = glob($path . '/**/*');

$all_files = $self_files + $recursive_files;
echo json_encode($all_files);
-> output on my js page 

JS: C:\xampp\htdocs\projects\Question Papers/2014.pdf
JS: C:\xampp\htdocs\projects\Question Papers/2015.pdf
JS: C:\xampp\htdocs\projects\Question Papers/2016.pdf
JS: C:\xampp\htdocs\projects\Question Papers/2017.pdf
JS: C:\xampp\htdocs\projects\Question Papers/2018.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Aeronautical
JS: C:\xampp\htdocs\projects\Question Papers/Civil
JS: C:\xampp\htdocs\projects\Question Papers/Electrical
JS: C:\xampp\htdocs\projects\Question Papers/Civil/2017.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Civil/2018.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Electrical/2014.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Electrical/2015.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Electrical/2016.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Electrical/2017.pdf
JS: C:\xampp\htdocs\projects\Question Papers/Electrical/2018.pdf

I'm expecting Something Like below

Question Papers {
       Civil{2014.pdf,2015.pdf,2016.pdf,2017.pdf,2018.pdf},
       Electrical{2014.pdf,2015.pdf,2016.pdf,2017.pdf,2018.pdf},
       Aeronautical{2014.pdf,2015.pdf,2016.pdf,2017.pdf,2018.pdf}
}

Can any one help me on how to achieve this in php or javascript.

vikatakavi
  • 97
  • 9
  • Does this answer your question? [How to get a list of files of subfolders and write them in a JSON using php?](https://stackoverflow.com/questions/34466362/how-to-get-a-list-of-files-of-subfolders-and-write-them-in-a-json-using-php) – Masoud Keshavarz Dec 31 '19 at 07:14
  • @MasoudKeshavarz I have one serious problem. `{"dir":[{"file":"2014.pdf"}],"name":'abcd' }` here i need to give dir as abcd and remove that name completely. `{"abcd":[{"file":"2014.pdf"}]"}` like this – vikatakavi Dec 31 '19 at 15:24

0 Answers0