0

我有一个代码来检查目录是否为空,以便我能够执行操作,但是这个简单的代码给出了一个错误:

警告: opendir(/Site/images/countries/abc/a/2.swf,/Site/images/countries/abc/a/2.swf) [function.opendir]: 系统找不到指定的路径。(代码:3)在 C:\wamp\www\Site\index.PHP 第 374 行

没有这样的文件

function IsNotEmpty($folder){
$files = array ();
if ( $handle = opendir ( $folder ) ) 
{
  while ( false !== ( $file = readdir ( $handle ) ) )
   {
      if ( $file != "." && $file != ".." ) 
         {
            $files [] = $file;
         }
   }

    closedir ( $handle ); 
 }
 return ( count ( $files ) > 0 ) ? TRUE: FALSE; }



 $dir ="/Site/images/countries/abc/a/2.swf";

 if (IsNotEmpty($dir)==true) 
     {
         echo "There is no such file";
 }
  else
     {
         echo "The file exists!";
     };

我不明白这里有什么问题。文件退出指定目录。

4

4 回答 4

1

opendir用于打开目录,而不是文件:-)

您也可以尝试暂时放入调试内容,以便查看发生了什么:

function IsNotEmpty ($folder) {
    $files = array ();
    if ($handle = opendir ($folder))  {
        echo "DEBUG opened okay ";
        while (false !== ($file = readdir ($handle))) {
            if ( $file != "." && $file != ".." ) {
                $files [] = $file;
                echo "DEBUG got a file ";
            }
        }
        closedir ($handle); 
    } else {
        echo "DEBUG cannot open ";
    }
    return (count($files) > 0 ) ? TRUE : FALSE;
}

$dir ="/Site/images/countries/abc/a";
if (IsNotEmpty($dir)) { 
    echo "There is no such file";
} else {
    echo "The file exists!";
}

如果这仍然不起作用并且您确定该目录存在(请记住,大小写对 UNIX 很重要),您可能需要查看该目录的权限以确保允许尝试访问它的用户 ID。

于 2011-06-20T13:01:00.273 回答
0

您可以使用以下代码段作为函数的主体:

$aFiles = glob($sFolder);
return (sizeof($aFiles) < 1) true : false;

这会将文件夹的内容作为数组获取,当为空时 - 您的目录为空。

于 2011-06-20T13:05:27.667 回答
0

试试这个:

function IsNotEmpty($dir) {
      $dir = rtrim($dir, '/').'/';
      return is_dir($dir) && count(glob($dir.'*.*') > 2);
}
于 2011-06-20T13:06:52.430 回答
0

克里斯汀,尝试删除尾部斜杠:

$dir ="/Site/images/countries/abc/a/"

变成

$dir ="/Site/images/countries/abc/a"
于 2011-06-20T13:07:03.987 回答