Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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 scandir错误:为foreach()提供的参数无效_Php - Fatal编程技术网

Php scandir错误:为foreach()提供的参数无效

Php scandir错误:为foreach()提供的参数无效,php,Php,有人能帮我解决这个问题吗?我似乎无法让它工作 <?php $file = scandir("../"); foreach ($files as $file) { if(is_dir($file) && $file != '.' && $file != '..' && $file != 'images') { $folders++; // Counter } elseif(is_file($file) &

有人能帮我解决这个问题吗?我似乎无法让它工作

<?php
$file = scandir("../");

foreach ($files as $file) {
    if(is_dir($file) && $file != '.' && $file != '..' && $file != 'images') {
        $folders++; // Counter
    } elseif(is_file($file) && $file != '.' && $file != '..' && $file != 'index.php') {
        $total++; // Counter
    }
}

echo "$folders folders · $total files";
?>
我得到的错误是:

为foreach提供的参数无效

在所有代码的顶端总是有帮助的

好吧,如果您得到的是foreach错误,但没有未定义的变量:files one,那么您似乎在scandir和循环之间的$files变量上做了一些坏事


或者你只是在愚弄所有运行其他代码的人,而不是发布在这里的代码

打字错误:$file=scandir../;应该是$files=-1由于缺乏基本的调试工作。此外,您不会初始化$folders,也不会将$totals用作$files。请重新检查变量名。如果$files是scandir生成的数组,那么您的foreach需要逐步完成,将$file设置为数组的每个值。要查看scandir的输出是否失败,请添加一个print_r$files;在您的foreach之前,它与print\u r$文件一起工作;当我这样做的时候,我得到了这些错误:未定义的变量:文件夹和未定义的变量:totalPlease在你的代码上想一想,想象一下没有这样的代码,谷歌:你将如何解决你的问题?未定义变量?首先定义它!在这种情况下,使用计数器,但不初始化it@FlameTrap如果你仍然得到你的foreach错误,这没关系。我有未定义的变量错误,但不再是foreach错误
error_reporting(E_ALL);