2

Sorry if my question is basic, because I dont familiar with php and json. I have create a php file that list a directory on my server and should print the result as JSON. So, how can I do that?

Here is my code to list files on directory:

<?php

$dir = "picture/";

if(is_dir($dir)){

    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){

            if($file == "." or $file == ".."){

            } else {
                echo $file."<br />";
                //echo json_encode($file);
            }
        }
    }
}

?>

Thank you for your response....

caknia
  • 1,221
  • 2
  • 13
  • 24

3 Answers3

9

I modified Francisco's code:

<?php
header('Content-Type: application/json');

$dir          = "./"; //path

$list = array(); //main array

if(is_dir($dir)){
    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){

            if($file == "." or $file == ".."){
                //...
            } else { //create object with two fields
                $list3 = array(
                'file' => $file, 
                'size' => filesize($file));
                array_push($list, $list3);
            }
        }
    }

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

    echo json_encode($return_array);
}

?>

Result:

{
    "files": [{
            "file": "element1.txt",
            "size": 10
        }, {
            "file": "element2.txt",
            "size": 10
        }
    ]
}
deadfish
  • 11,996
  • 12
  • 87
  • 136
6

Maybe try something like this?

<?php

$dir          = "picture/";
$return_array = array();

if(is_dir($dir)){

    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){

            if($file == "." or $file == ".."){

            } else {
                $return_array[] = $file; // Add the file to the array
            }
        }
    }

    echo json_encode($return_array);
}

?>
francisco.preller
  • 6,559
  • 4
  • 28
  • 39
  • yap, now the question is how can I add the id for each string on there... so maybe the json look like this..? [{"key1":"value1"},{"key1":"value1"},........,{"key1":"value1"}] – caknia Apr 08 '13 at 02:23
  • You can add this as the key during your loop. So where you see $return_aray[] = $filoe; You would replace it with $return_array[$key] = $file; Obviously, you will have to update your code to change $key to reflect the key name which you wish to display. – francisco.preller Apr 08 '13 at 02:26
  • so if I want to print 2 keys for each, can represent it like this..? $return_array[$key1][$key2] = $file – caknia Apr 08 '13 at 02:32
  • That would result int [{key1: {key2: value}}]. I think what you want might be something more along the lines of [{key1: [val1, val2, val3]},{key2: [val1, val2, val3]}]? – francisco.preller Apr 08 '13 at 02:35
  • That would be simple, you would then just insert another array as the value for that key, so something like $result_array[$key1] = array($val1, $val2, $val3); – francisco.preller Apr 08 '13 at 02:38
  • Oh, I see, let me try it first. If I have any trouble I will ask again... :D thank you... :) – caknia Apr 08 '13 at 02:40
  • What does $dh stand for? – knot22 Jul 23 '16 at 02:02
3

You can use this code to name array and to set the same key:

<?php

$dir = "../uploads";
if(is_dir($dir)){
    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){
            if($file != "." and $file != ".."){
                $files_array[] = array('file' => $file); // Add the file to the array
            } 
        }
    }
    $return_array =array('name_array' => $files_array);

    exit (json_encode($return_array));
}


?>
gopeca
  • 1,601
  • 12
  • 12