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},
Mr.mubanga
  • 41
  • 1
  • 7

1 Answers1

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);

 }


?>
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
R B
  • 658
  • 4
  • 12
  • @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 '18 at 09:53
  • what format can you show me which format you want – R B Mar 30 '18 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 '18 at 10:05
  • now what type format you getting for this json_encode($return_array) – R B Mar 30 '18 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 '18 at 10:16
  • its coming proper see your list array keys and values – R B Mar 30 '18 at 10:19
  • when i test it using online json viewer its saying wrong format...? help me understand – Mr.mubanga Mar 30 '18 at 10:29
  • https://www.w3schools.com/js/js_json_intro.asp read this article – R B Mar 30 '18 at 10:32