2

私はphpとjsonに慣れていないので、私の質問が基本的なものであれば申し訳ありません。サーバー上のディレクトリをリストするphpファイルを作成し、結果をJSONとして出力する必要があります。それで、どうすればそれを行うことができますか?

ディレクトリ上のファイルを一覧表示するコードは次のとおりです。

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

?>

ご回答ありがとうございます....

4

3 に答える 3

6

たぶん、このようなことを試してみませんか?

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

?>
于 2013-04-08T02:07:01.030 に答える