0

我有以下代码。

    if ($_GET['dir']!=''){
        $checkifhasfiles = array_diff(scandir($_GET['dir']), array(".","..","error_log"));
        foreach($checkifhasfiles as $cihf){
            if(is_file($_GET['dir'].'/'.$cihf)){
                echo "Ok, the folder has files";
            }
        }
    }

我想要做的只是one在文件夹包含only文件时才显示消息。问题是,正如预期的那样,foreach 正在为文件夹包含的每个文件“回显”一条消息。

我怎样才能“绕过”它,如果文件夹有文件,只打印一条消息?

谢谢和最好的问候

顺便说一句 - 很抱歉缩进,我在记事本上写,因为我不在家,而且缩进不完美

4

3 回答 3

2

如果我理解正确,您不只是想在第一个找到的文件上回显,而是如果该文件夹仅包含文件(即没有子文件夹)。如果这是真的,这将起作用:

if (isset($_GET['dir']) && !empty($_GET['dir']))
{
    $checkifhasfiles = array_diff(scandir($_GET['dir']), array(".","..","error_log"));
    $i = 0;

    foreach($checkifhasfiles as $cihf)
    {
        if(is_file($_GET['dir'].'/'.$cihf))
        {
            $i++;
        }
    }

    if ( count($checkifhasfiles) === $i )
    {
        echo "Ok, the folder has files";
    }
}
于 2013-09-19T12:01:51.247 回答
1

你可以试试“休息”吗?http://php.net/manual/en/control-structures.break.php

if ($_GET['dir']!=''){
     $checkifhasfiles = array_diff(scandir($_GET['dir']), array(".","..","error_log"));
     foreach($checkifhasfiles as $cihf){
         if(is_file($_GET['dir'].'/'.$cihf)){
            echo "Ok, the folder has files";break;
         } 
      }  
   }
于 2013-09-19T11:56:30.907 回答
1

更容易检查文件夹是否有孩子:

if(count(glob($_GET['dir']."/*")) { 
  echo "NOT EMPTY"; 
}
于 2013-09-19T11:58:55.370 回答