检查文件夹是否为空或不是php


Check if folder is empty or not php

我必须检查我的文件夹资产/文件是否为空,以显示创建新文件的消息或列出文件夹中的所有文件。。但我的文件夹是空的,我收到消息"你有文件"。。

<?php
 /**
 * FileExist
 * check files
 * @return no
 */
public function fileExist()
{
    $open = "../asset/files";
    if (scandir($open)) {
        echo "`You got files";
        print_r(scandir($open));
    } else {
        echo "You haven't got files";
    }
}
?>

这应该适用于您:

(这里我只使用glob()来获取目录的所有文件,并检查它是否为空。您的方法不起作用,因为scandir()还包括...

public function fileExist()
{
    $open = "../asset/files";
    if ($files = glob($open . "/*")) {
        echo "You got files";
        print_r($files);
    } else {
        echo "You haven't got files";
    }
}

这应该适用于您,并且是检查文件夹是否为空的简单方法。首先,我使用php的scandir()函数扫描目录,然后计算扫描目录。如果它等于2,则它的平均值在文件夹中没有文件,如果它大于或小于2,则其平均值在文件夹中有文件

$scan=scandir($dir);

if(count($scan)!=2){
echo 'you have files';
}
}else{
echo 'there is no file in directory';
}