【问题标题】:return PHP data as JSON Object将 PHP 数据作为 JSON 对象返回
【发布时间】:2015-11-09 18:16:13
【问题描述】:

我有一个 PHP 脚本,它必须为包含在特定目录中的所有文件返回 3 个内容。

  1. 文件名
  2. 文件大小
  3. 文件创建时间

我可以为每个文件回显这三个值,但我想以 JSON 格式返回所有这些数据。将所有这些数据转换为 JSON 格式的最佳方法是什么?

function listAllFiles($dir)
{
    $format = "d/m/y h:m";
    $filesInfo;
    if (is_dir($dir))
   {
        if ($dh = opendir($dir))
        {
            while (($file = readdir($dh)) !== false)
           {
               if ($file != "." && $file != "..") 
               {   
                   echo findFileSize($dir.'/'.$file)."      ".date($format, filemtime($dir.'/'.$file))."     ";
                   echo $file.'<br>';

               }     

           } 
         }
     closedir($dh);


   }
    else {
       print 'folder not found';
    }
}

【问题讨论】:

  • 最好的方法是使用json_encode(),但是你是如何获取这些数据元素的呢?
  • 1) 向我们展示您迄今为止所做的尝试。 2) 显示代码 3) 你有没有尝试过像 PHP Script to json object 这样的谷歌搜索?
  • 我已经添加了php代码...
  • 构建一个值数组而不是回显它们..... json_encode 该数组以给出一个字符串,然后将其回显到前端
  • 我推荐一个二维数组,每个文件都有一个枚举条目,文件详细信息(名称/大小/日期/等)作为关联

标签: javascript php json getjson


【解决方案1】:

函数

  1. 文件名

Use glob() to find the files in a directory

  1. 文件大小

Use filesize() to find the size of a file

  1. 文件创建时间

Use filectime() to find the last creation time of a file

将所有这些数据转换为 JSON 格式的最佳方法是什么?

Use json_encode() to convert a PHP array to a JSON array

代码示例

function listAllFiles($dir){
    if(!isdir($dir)) { print "Folder not found"; return; }

    $files = glob($dir);
    $arr = array();
    foreach($files as $file){
        $file = array();

        //Get filename
        $file["name"] = $file;

        //Get filesize
        $file["size"]= filesize($file);

        //Get file creation time
        $file["creation_time"] = filectime($file);

        array_push($arr, $file);
    }

    $json = json_encode($arr);
    return $json;
}

echo listAllFiles("/folder/");

【讨论】:

    【解决方案2】:

    PHP中有2个简单的json函数json_encode()json_decode()

    json_encode() 将 PHP 数组或对象转换为 JSON 字符串,因此请创建一个数组或对象来包含所有数据,然后将 json_encode() 的结果回显给调用应用程序。

    function listAllFiles($dir) {
    
        $results = array();
        $results['error'] = false;
    
        $format = "d/m/y h:m";
        $filesInfo;
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                   if ($file != "." && $file != "..") {   
    
                       // create an object to hold data for this file
                       $o = new stdClass();
                       $o->filesize = findFileSize($dir.'/'.$file)
                       $o->filedate = date($format, filemtime($dir.'/'.$file));
                       $o->filename = $file;
    
                       // put this object into the results array
                       $results[] = $o;
                   }     
                } 
            }
            closedir($dh);
       } else {
           $results['error'] = true;
           $results['err_msg'] = 'folder not found';
        }
    
        return $results;
    }
    
    
    $result = listAllFiles('a/b/c');
    echo json_encode($result);
    

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多