Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP JSON返回字符串问题_Php_Iphone_Json - Fatal编程技术网

PHP JSON返回字符串问题

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,所以另一个开发人员帮我编写了这段代码。我正在尝试返回服务器上文件夹中所有文件的名称。然后将这些数据传递到我的iPhone应用程序,该应用程序使用这些数据。但是,我在文件夹中有160个文件,JSON字符串只返回85。此代码是否有问题:

 <?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
    。它应该列出所有匹配的文件。查看是否可以识别未列出的文件

如果是在类UNIX系统上,则文件区分大小写。可能是*.jpg会匹配,而*.jpg或*.jpg不会匹配

以下函数遍历$path中的所有文件,并仅返回符合条件的文件(不区分大小写):



您能否编辑返回的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;
}
?>