检查文件夹是否为空输出错误(PHP)

发布于 2024-11-16 01:20:56 字数 900 浏览 4 评论 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!";
     };

我不明白这里出了什么问题。文件存在于指定目录中。

I have a code to check if directory is empty, so that i will be able to perform actions, but this simple code gives an error:

Warning: opendir(/Site/images/countries/abc/a/2.swf,/Site/images/countries/abc/a/2.swf) [function.opendir]: The system cannot find the path specified. (code: 3) in C:\wamp\www\Site\index.PHP on line 374

There is no such file

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!";
     };

I don't understand what is wrong here. The file exits in the specified directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千笙结 2024-11-23 01:20:56

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。

opendir is for opening directories, not files :-)

You can also try temporarily putting in debug stuff so that you can see what's happening:

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!";
}

If that's still not working and you're sure the directory exists (remember, case is important for UNIX), you may want to look into the permissions on that directory to ensure that the user ID trying to access it is allowed.

贱人配狗天长地久 2024-11-23 01:20:56

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

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

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

You chould use the following snippet as body for your function:

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

This will get the contents of the folder as an array, when empty - your directory is empty.

咆哮 2024-11-23 01:20:56

试试这个:

function IsNotEmpty($dir) {
      $dir = rtrim($dir, '/').'/';
      return is_dir($dir) && count(glob($dir.'*.*') > 2);
}

Try for instance this:

function IsNotEmpty($dir) {
      $dir = rtrim($dir, '/').'/';
      return is_dir($dir) && count(glob($dir.'*.*') > 2);
}
美男兮 2024-11-23 01:20:56

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

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

成为

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

Christine, try removing the trailing slash:

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

Becomes

$dir ="/Site/images/countries/abc/a"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文