0

have written a class that uses in built php function DirectoryIterator which as to show folder structure like this

root
   |__subfolder
   |          |_mp3 
   |          |_png
   |__subfolder2
              |_mp3
              |_mp3 

Here is my class

 <?php
  $list = array();

 $dir = new DirectoryIterator('/opt/lampp');
 foreach ($dir as $fileinfo)
 {
 if ($fileinfo->isFile())
  {
    //echo $fileinfo->getBasename() . "\n";
   //echo $fileinfo->getBasename('.jpg') . "\n";
  }
  else
   { //create object with two fields
            $list3 = array( 'name'=> $fileinfo->getFilename(),
                            'type'=> $fileinfo->getType(),
                            'date'=> $fileinfo->getMTime(),
                            'size'=> $fileinfo->getSize());
             array_push($list, $list3);



    }


        $return_array = array('files'=>$list);
        echo json_encode(  $return_array)
     }
     ?>

have written the echo json_encode but the string format does not seem to be correctly writen

here is a view of the string format of the out put, am using online json viewer for verification

 {"files":[{"name":"info","type":"dir","date":1491647493,"size":4096}]}     {"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096}]}{"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096},
5
  • use json_encode to convert it to json :D
    – JimL
    Mar 30, 2018 at 9:30
  • @JimL, thanks for your response,how can i archive that, am new to json...?
    – Mr.mubanga
    Mar 30, 2018 at 9:32
  • @MunimMunna thanks for your response, where am i missing some commas..?help me understand
    – Mr.mubanga
    Mar 30, 2018 at 11:40
  • My mistake, you are not missing comma's, but you are encoding the sub-folders i.e $list_array, you need to insert all $return_array into $list array, then encode the $list array outside the for-loop at the bottom. Mar 30, 2018 at 11:48

1 Answer 1

0

use json_encode()

<?php
  $list = array();

 $dir = new DirectoryIterator('/opt/lampp');
 foreach ($dir as $fileinfo)
 {
 if ($fileinfo->isFile())
  {
    //echo $fileinfo->getBasename() . "\n";
   //echo $fileinfo->getBasename('.jpg') . "\n";
  }
  else
   { //create object with two fields
            $list3 = array( 'name'=> $fileinfo->getFilename(),
                            'type'=> $fileinfo->getType(),
                            'date'=> $fileinfo->getMTime(),
                            'size'=> $fileinfo->getSize());
             array_push($list, $list3);



    }


        $return_array = array('files'=>$list);
        echo json_encode($return_array);

 }


?>
8
  • @Hassan thanks for your response , the strings been returned by the json_encode($return_array) is not correctly formatted as a json string
    – Mr.mubanga
    Mar 30, 2018 at 9:53
  • what format can you show me which format you want
    – R B
    Mar 30, 2018 at 10:00
  • thansk for your response, the formart i want is this { "data" : [ { "data":"audio","children" : [ {"data" : {"title":"juicy.mp3"},"attr":{"href": "audio/juicy.mp3","id": "1239"}, "icon": "images\/mp3-icon.gif"},
    – Mr.mubanga
    Mar 30, 2018 at 10:05
  • now what type format you getting for this json_encode($return_array)
    – R B
    Mar 30, 2018 at 10:11
  • {"data":[{"name":"info","type":"dir","date":1491647493,"size":4096}]}{"data":[{"name":"info","type":"dir","date":1491647493,"size":4096},
    – Mr.mubanga
    Mar 30, 2018 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.