我使用以下脚本来读取目录.如果目录中没有文件,则应该为空.问题是,它只是一直说目录是空的,即使内部有ARE文件,反之亦然.
<?php
$pid = $_GET["prodref"];
$dir = '/assets/'.$pid.'/v';
$q = (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
if ($q=="Empty")
echo "the folder is empty";
else
echo "the folder is NOT empty";
?>
Run Code Online (Sandbox Code Playgroud)
You*_*nse 123
看来你需要scandir代替glob,因为glob看不到unix隐藏文件.
<?php
$pid = basename($_GET["prodref"]); //let's sanitize it a bit
$dir = "/assets/$pid/v";
if (is_dir_empty($dir)) {
echo "the folder is empty";
}else{
echo "the folder is NOT empty";
}
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
return (count(scandir($dir)) == 2);
}
?>
Run Code Online (Sandbox Code Playgroud)
请注意,此代码不是效率的顶峰,因为没有必要只读取所有文件来判断目录是否为空.所以,更好的版本将是
function dir_is_empty($dir) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return FALSE;
}
}
closedir($handle);
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,不要使用单词来替换布尔值.后者的目的是告诉你某些事情是否空洞.一个
a === b
Run Code Online (Sandbox Code Playgroud)
表达式已经返回Empty或者Non Empty在编程语言方面,FALSE或者TRUE分别 - 因此,您可以在控制结构中使用非常结果,例如IF()没有任何中间值
flu*_*flu 65
我认为使用FilesystemIterator应该是最快速,最简单的方法:
// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();
Run Code Online (Sandbox Code Playgroud)
或者在实例化时使用类成员访问:
// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为new FilesystemIterator将最初指向文件夹中的第一个文件 - 如果文件夹中没有文件,valid()则返回false.(见这里的文档.)
正如abdulmanov.ilmir所指出的那样,在使用之前可选择检查目录是否存在,FileSystemIterator否则它会抛出一个UnexpectedValueException.
我找到了一个快速解决方案
<?php
$dir = 'directory'; // dir path assign here
echo (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
?>
Run Code Online (Sandbox Code Playgroud)
使用
if ($q == "Empty")
Run Code Online (Sandbox Code Playgroud)
代替
if ($q="Empty")
Run Code Online (Sandbox Code Playgroud)
这是一个非常古老的线程,但我想我应该给我十美分。其他解决方案对我不起作用。
这是我的解决方案:
function is_dir_empty($dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if($fileInfo->isDot()) continue;
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
简短而甜蜜。奇迹般有效。
尝试这个:
<?php
$dirPath = "Add your path here";
$destdir = $dirPath;
$handle = opendir($destdir);
$c = 0;
while ($file = readdir($handle)&& $c<3) {
$c++;
}
if ($c>2) {
print "Not empty";
} else {
print "Empty";
}
?>
Run Code Online (Sandbox Code Playgroud)
对于使用一个面向对象的方法RecursiveDirectoryIterator从标准PHP库(SPL) 。
<?php
namespace My\Folder;
use RecursiveDirectoryIterator;
class FileHelper
{
/**
* @param string $dir
* @return bool
*/
public static function isEmpty($dir)
{
$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
return iterator_count($di) === 0;
}
}
Run Code Online (Sandbox Code Playgroud)
无需在需要时创建实例FileHelper,您可以在任何需要的地方访问此静态方法,如下所示:
FileHelper::isEmpty($dir);
Run Code Online (Sandbox Code Playgroud)
的FileHelper类可以与用于复制,删除,重命名等其他有用的方法被扩展
不需要检查方法内目录的有效性,因为如果它无效, 的构造函数RecursiveDirectoryIterator将抛出一个UnexpectedValueException足以覆盖该部分的 。
我用了:
if(is_readable($dir)&&count(scandir($dir))==2) ... //then the dir is empty
Run Code Online (Sandbox Code Playgroud)