list files php from - create json file

前端 未结 2 622
情话喂你
情话喂你 2021-01-24 10:55

I want to create a php script that loads all my files (.pdf) from the 3 directory and create a json file.

I try this

$dir_2016 = \"./HCL/2016\";
$dir_201         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 11:04

    You should move your definition of $json_file to bottom as follow:

    // ... get files code
    $json_file = array(
        "2016" => $files_2016,
        "2015" => $files_2015,
        "2014" => $files_2014,
    );
    echo json_encode($json_file);
    

    Because array is passing by value rather than passing by reference.

    And, a better way to get files and sub-directories in a directory shallowly is use scandir, for example:

    $files_2014 = array_slice(scandir('./HCL/files_2014'), 2)
    

    See: http://php.net/manual/en/function.scandir.php

提交回复
热议问题