我有以下代码。
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";
}
}
}
我想做的是仅在文件夹包含only
文件时才显示one
消息。问题是,正如预期的那样,foreach 正在为文件夹包含的每个文件"回显"一条消息。
我如何"绕过"它,如果文件夹有文件,只打印一条消息?
感谢和最诚挚的问候
顺便说一句 - 对不起缩进,我把它写在记事本上,因为我不在家,而且它没有完全缩进
如果我理解正确,您不仅想在找到的第一个文件上回显出来,而且如果文件夹仅包含文件(即没有子文件夹)。如果这是真的,这将起作用:
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";
}
}
你能尝试一下"休息"吗? 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;
}
}
}
更容易检查文件夹是否有子文件夹:
if(count(glob($_GET['dir']."/*")) {
echo "NOT EMPTY";
}