Php 使用android中的json获取服务器上文件夹的文件列表

Php 使用android中的json获取服务器上文件夹的文件列表,php,android,mysql,json,Php,Android,Mysql,Json,我正在使用以下代码获取服务器上文件夹的文件列表: <?php if ($handle = opendir('.pic')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { //$thelist = $thelist.$file.'<br>'; $thelist =

我正在使用以下代码获取服务器上文件夹的文件列表:

<?php
 if ($handle = opendir('.pic')) {
   while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            //$thelist = $thelist.$file.'<br>';
            $thelist = $thelist.$file."\r\n";
          }
       }
  closedir($handle);
  }
 echo $thelist;
?>
但我不知道如何用json数组像mysql一样更改文件名

更新: 我尝试以下代码:

<?php
 $thelist = array();
 if ($handle = opendir('.pic')) {
   while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist[] = $file;
          }
       }
  closedir($handle);
  }
 echo json_encode($thelist);

制作一个文件名数组,并对其进行json\u编码:

<?php
 $thelist = array();
 if ($handle = opendir('.pic')) {
   while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist[] = $file;
          }
       }
  closedir($handle);
  }
 echo json_encode($thelist);
一般来说,我更喜欢使用
glob()
从文件夹中获取文件(和/或文件夹)列表

首先,您应该初始化一个数组,然后再附加到该数组

这是我的解决方案:

<?php
$list = []; // Since 5.4.x you can use shorthand syntax to init an array
$dir = dirname(__FILE__); // Just to get the path to current dir

// glob returns an array with the files it found matching the search pattern (* = wildcard)
$files = glob($dir."/.pic/*");

// Looping through all the files found in ./.pic
foreach ($files as $file) {
    // Removing the full path and appending the file to the $list array.
    // Now it will look like ".pic/filename.ext"
    $list[] = str_replace($dir."/", "", $file); 
}
echo json_encode($list, JSON_PRETTY_PRINT); // JSON_PRETTY_PRINT for beautifying the output
?>

另一方面,
glob()
可以提供更多选项,您可以在

建议更新nr 1 这个json的输出将是一个json数组,正如错误消息所说的那样。解决方案可以是将
JSONObject json
更改为
JSONArray json

另一方面,通过在代码中做一些小的调整,可以使PHP脚本的输出成为json对象而不是数组

而不是
echo json\u encode($list,json\u PRETTY\u PRINT)
,只需将
$list
添加到另一个数组中,如下所示:
echo json\u encode(['files'=>$list],json\u PRETTY\u PRINT)

这将使解析器将其视为json对象,因为数组具有字符串键(
files
),而不是索引键(
0,1,…,n

<?php
 $thelist = array();
 if ($handle = opendir('.pic')) {
   while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist[] = $file;
          }
       }
  closedir($handle);
  }
 echo json_encode($thelist);
<?php
$list = []; // Since 5.4.x you can use shorthand syntax to init an array
$dir = dirname(__FILE__); // Just to get the path to current dir

// glob returns an array with the files it found matching the search pattern (* = wildcard)
$files = glob($dir."/.pic/*");

// Looping through all the files found in ./.pic
foreach ($files as $file) {
    // Removing the full path and appending the file to the $list array.
    // Now it will look like ".pic/filename.ext"
    $list[] = str_replace($dir."/", "", $file); 
}
echo json_encode($list, JSON_PRETTY_PRINT); // JSON_PRETTY_PRINT for beautifying the output
?>