You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP判断目录是否为空的问题求助

PHP判断目录是否为空的问题求助

我最近在尝试用PHP判断一个目录是否为空,找了不少类似的问题但还是没解决。现在遇到的问题是,不管目录实际空不空,我写的代码都返回“目录不为空”的结果。

我试过这段代码:

<?php

function is_dir_empty($dir) {
    if (!is_readable($dir)) return NULL;
    $files = array_diff(scandir($dir), array('.', '..'));
    return empty($files);
}

$dir = 'images/tmp';

if (is_dir_empty($dir)) {
    echo "The folder is empty";
} else {
    echo "The folder is NOT empty";
}

?>

目录权限是755,路径也确认是对的。后来我又换了另一个函数试,结果还是一样:

function is_dir_empty($dir) {
    if (!is_readable($dir)) return NULL;
    $files = glob("$dir/*");
    return empty($files);
}

有没有大佬能帮我看看问题出在哪呀?

备注:内容来源于stack exchange,提问作者Midhat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:44:35