如何返回不包含特定文件的文件夹?

发布于 2024-11-09 05:16:07 字数 578 浏览 5 评论 0原文

我想过滤掉不包含海报.* (JPG/PNG/GIF) 或文件夹.* (JPG/PNG/GIF) 的文件夹。 我创建了以下代码,但我对如何有效地执行此操作有点无能为力:

<?php
$dir = "/share/test/";
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." &&
            $file != ".." &&
            $file != ".DS_Store" &&
            $file != "_Incoming" &&
            is_dir($dir.$file) &&
            !file_exists($dir.$file."/poster.*") ){

                echo "$file\n";

        }
    }
    closedir($handle);
}
?>

谢谢!

I'd like to filter out folders that do not contain poster.* (JPG/PNG/GIF) OR folder.* (JPG/PNG/GIF).
I created the following code but I'm a bit clueless on how to do this efficiently:

<?php
$dir = "/share/test/";
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." &&
            $file != ".." &&
            $file != ".DS_Store" &&
            $file != "_Incoming" &&
            is_dir($dir.$file) &&
            !file_exists($dir.$file."/poster.*") ){

                echo "$file\n";

        }
    }
    closedir($handle);
}
?>

Thanks!

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

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

发布评论

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

评论(1

舂唻埖巳落 2024-11-16 05:16:07

就到这里,希望有帮助:

<?php 
/**
 * Recursive function that will check all folders in the 
 * requested directory for keywords [$lookfor] [Any extention]
 * true,false on returning notfounds 
 *
 * @param $path to folder
 * @param $lookfor word in filename or folder
 * @param $showNotfounds [true|false]
 * @return echo'ed out
 */
function lookfor($path,$lookfor,$showNotfounds=false){
    if(file_exists($path) && is_readable($path)){}else{die('Error reading folder ./'.$path.'');}
    if ($handle = @opendir($path)) {
        while ($file = readdir($handle)){

            if ($file=='.' || $file=='..'){}else{
                if (is_dir($path."/".$file)){
                    //Recursive
                    if(stristr($file, $lookfor) === FALSE) {
                        //[folder]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[folder]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                    lookfor($path."/".$file,$lookfor,$showNotfounds);
                }else{
                    if(stristr($file, $lookfor) === FALSE) {
                        //[file]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[file]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                }
            }
        }
        closedir($handle);
    }
}

//example usage
lookfor('.','folder.',true);
echo '<hr>';
lookfor('.','poster.',true);
echo '<hr>';
lookfor('.','poster.jpg',false);

?>

here ya go, hope it helps:

<?php 
/**
 * Recursive function that will check all folders in the 
 * requested directory for keywords [$lookfor] [Any extention]
 * true,false on returning notfounds 
 *
 * @param $path to folder
 * @param $lookfor word in filename or folder
 * @param $showNotfounds [true|false]
 * @return echo'ed out
 */
function lookfor($path,$lookfor,$showNotfounds=false){
    if(file_exists($path) && is_readable($path)){}else{die('Error reading folder ./'.$path.'');}
    if ($handle = @opendir($path)) {
        while ($file = readdir($handle)){

            if ($file=='.' || $file=='..'){}else{
                if (is_dir($path."/".$file)){
                    //Recursive
                    if(stristr($file, $lookfor) === FALSE) {
                        //[folder]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[folder]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                    lookfor($path."/".$file,$lookfor,$showNotfounds);
                }else{
                    if(stristr($file, $lookfor) === FALSE) {
                        //[file]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[file]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                }
            }
        }
        closedir($handle);
    }
}

//example usage
lookfor('.','folder.',true);
echo '<hr>';
lookfor('.','poster.',true);
echo '<hr>';
lookfor('.','poster.jpg',false);

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