I don't know any PHP so another developer helped me out with this code. I am trying to return the names of all the files in a folder on my server. These are then passed to my iPhone app which uses the data. However, I have 160 files in the folder and the JSON string only returns 85. Is there something wrong with 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);
?>
There is no reason this code should fail. However, your $path variable should not end in a slash (as you have that in the glob call).
Things to look at:
print_r on the $files variable. It should list all the matched files. See if you can identify the files that aren't listed.If this is on a UNIX-like system, your files are case-sensitive. It might be that *.jpg will match, whereas *.JPG or *.jpG won't.
The following function goes through all files in $path, and returns only those which match your criteria (case-insensitive):
<?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;
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With