我试图通过使用 scandir
并从数组中删除文件来获取路径中的目录列表,但我的 echo 出现错误。
我正在使用代码
$path = "Calltypes/";
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
for($i = 0; $i < sizeof($files); $i++) {
if(!is_dir($files[$i]))
array_splice($files, $i, 1);
}
echo sizeof($files);
这是回显输出:
Notice: Undefined offset: 0 in C:\xampp\htdocs\index.php on line 32
2
Calltypes/路径有3个文件夹和1个txt文件。
编辑:第 32 行是
if(!is_dir($files[$i]))
最佳答案
您可以使用foreach
代替for
。之后使用 unset
因为 array_splice
会重新排列键所以你会得到错误
$path = "Calltypes/";
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $key => $value) {
if(!is_dir($path.$files[$key]))
unset($files[$key]);
}
echo sizeof($files);
关于php - 使用 PHP 从文件和目录数组中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719672/