PHP JSON Return String Question

I do not know any PHP, so another developer helped me with this code. I am trying to return the names of all the files in a folder on my server. Then they are transferred to my iPhone application that uses data. However, I have 160 files in the folder, and the JSON string returns 85. Is there anything not in this code:

 <?php
$path = 'Accepted/';

# find all files with extension jpg, jpeg, png 
# note: will not descend into sub directorates
$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);

// output to json
echo json_encode($files);

?>
+3
source share
2 answers

There is no reason to refuse this code. However, your variable $pathshould not end with a slash (as is the case in the call glob).

What to watch:

  • Are you sure all the files are .jpg, .jpeg or .png files?
  • , .JPG,.JPEG .PNG( Unix/Linux).
  • print_r $files. . , , .
+2

UNIX- , . , *.jpg , *.JPG *.jpG .

$path , ( ):

<?php
$path = 'Accepted/';
$matching_files = get_files($path);
echo json_encode($matching_files);

function get_files($path) {
    $out = Array();
    $files = scandir($path); // get a list of all files in the directory
    foreach($files as $file) {
         if (preg_match('/\.(jpg|jpeg|png)$/i',$file)) {
             // $file ends with .jpg or .jpeg or .png, case insensitive
             $out[] = $path . $file;
         }
    }
    return $out;
}
?>
+2

Source: https://habr.com/ru/post/1717725/


All Articles