【问题标题】:How to check if folder has at least 1 file in php如何检查文件夹是否在php中至少有1个文件
【发布时间】:2016-10-07 03:10:05
【问题描述】:

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

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

我只想检查其中的文件

【问题讨论】:

标签: php


【解决方案1】:

您可以通过使用opendir 和readdir 来实现它

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

【讨论】:

  • $result = $file !== "." && $file !== ".." && !is_dir($file);
  • @DarkBee 没错。它会检查 child 不是目录!!
  • @Saty 当其中有文件或子目录时,这给了我 1。如果里面有文件,它应该只给出 1
  • 是的,它将满足检查文件夹内至少 1 个文件的要求
  • 所以我需要一种方法来检查文件夹中是否只有文件;不是子目录
猜你喜欢
  • 2017-06-30
  • 1970-01-01
  • 2011-02-12
  • 2011-11-15
  • 2022-01-23
  • 1970-01-01
  • 2013-02-21
  • 2010-12-10
  • 1970-01-01
相关资源
最近更新 更多