1:
2:
function isEmptyDir( $path )
{
$dh= opendir( $path );
while(false !== ($f = readdir($dh)))
{
if($f != ". " && $f != ".. " )
return true;
}
return false;
}2:
$root = dirname(__FILE__);
$root = str_replace("\\", "/", $root);
$path = $root.'/test/';
$isempty = file_exit();
//检查目录是否为空
function file_exit($filelastname = ''){
global $path;
if($filelastname != ''){
$handle = opendir($path.$filelastname);
}else{
$handle = opendir($path);
}
while (false !== ($file = readdir($handle))) {
if($file == '.' || $file == '..'){
continue;
}
$file_array[] = $file;
}
if($file_array == NULL){//没有文件
closedir($handle);
return false;
}
closedir($handle);
return true;//有文件
}
本文介绍了一种使用PHP检查目录是否为空的方法。通过定义函数isEmptyDir()来遍历指定路径下的所有文件,如果发现除了.和..之外还有其他文件存在,则认为该目录不为空。此外,还提供了一个file_exit()函数用于更灵活地指定要检查的目录。
374

被折叠的 条评论
为什么被折叠?



