如何检查文件夹中是否至少有1个文件

时间:2016-06-07 09:32:01

标签: php

我想检查文件夹是否至少包含1个文件。 我现在用它来检查文件夹是否为空,但这也检查文件夹中是否有子文件夹。

if (count(glob($dir.'/*')) === 0 ) {}

我只想查看其中的文件

1 个答案:

答案 0 :(得分:2)

您可以使用opendirreaddir

来实现这一目标
function check_file($dir) {
    $result = false;
    if($dh = opendir($dir)) {
        while(!$result && ($file = readdir($dh)) !== false) {
            $result = $file !== "." && $file !== ".." && !is_dir($file);
        }

        closedir($dh);
    }

    return $result;
}

$dir="/var/www/html/jkjkj/";// your path here
echo check_file($dir);// return 1 if file found otherwise empty
相关问题