PHP JSON返回字符串问题
我不懂PHP,所以另一个开发人员帮我编写了这段代码。我正在尝试返回服务器上文件夹中所有文件的名称。然后将这些数据传递到我的iPhone应用程序,该应用程序使用这些数据。但是,我在文件夹中有160个文件,JSON字符串只返回85。此代码是否有问题:PHP JSON返回字符串问题,php,iphone,json,Php,Iphone,Json,我不懂PHP,所以另一个开发人员帮我编写了这段代码。我正在尝试返回服务器上文件夹中所有文件的名称。然后将这些数据传递到我的iPhone应用程序,该应用程序使用这些数据。但是,我在文件夹中有160个文件,JSON字符串只返回85。此代码是否有问题: <?php $path = 'Accepted/'; # find all files with extension jpg, jpeg, png # note: will not descend into sub directorates
<?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);
?>
此代码没有失败的原因。但是,您的
$path
变量不应以斜杠结尾(正如您在glob
调用中所做的那样)
需要注意的事项:
- 您确定所有文件都是.jpg、.jpeg或.png文件吗
- 您确定某些文件不是.JPG、.JPEG或.PNG(Unix/Linux上的大小写很重要)吗
- 在
变量上尝试$files
。它应该列出所有匹配的文件。查看是否可以识别未列出的文件print\r
您能否编辑返回的JSON字符串和文件夹中的文件列表以回答您的问题?您是否检查了glob
是否返回了正确的值?谢谢,一些文件名已大写。
<?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;
}
?>