将空文件夹添加到不允许的数组中

发布于 2024-10-30 09:06:46 字数 6671 浏览 2 评论 0原文

我正在使用一个数组,它允许我防止特定的子文件夹包含在生成音频播放列表的脚本中。它看起来像这样:

$disallowed=array('..', '.', 'simon', 'steve');

我想找到一种方法来禁止任何空文件夹,或者更好的是,禁止其中没有任何 MP3 的文件夹(如果脚本将文件夹解释为非空,如果它们包含系统文件自动生成)。

我在下面包含了完整的脚本,以便您可以看到不允许的数组如何适应。

有人可以帮助解决这个问题吗?

谢谢,

尼克

<?php

/*
* =====================
* FUNctions
* =====================
*/

/*
* array subvalue sort -- from: http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
*
* this function lets me sort the multidimensional array containing song/artist information by the file modified time, a subvalue
*/
function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    arsort($b); //change this to 'asort' for ascending
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}

/*
* function written to clean up my messy code (too many slashes ... display '/' as '&raquo' (>>) for user friendliness )
*/
function clean($dirty, $type='general', $debug=false){

    //debug
    if($debug==true)
        echo'<br />value before clean: '.$dirty.' (first character: '.substr($dirty, 0, 1).')';

    /*
    * General cleaning -- remove '/' at front and end
    */
    if(substr($dirty, 0, 1)=='/'){
        //echo'<br />found leading /';
        $dirty=substr($dirty, 1, strlen($dirty)-1);
    }

    if(substr($dirty, -1)=='/')
        $dirty=substr($dirty, 0, strlen($dirty)-1);


    //prepare the subfolder display information by type
    if($type=='link')
        $dirty=str_replace(array('//','&raquo;'), array('/', '/'), $dirty); 

    else if($type=='display')
        $dirty=str_replace(array('///','//','/'), array('&raquo;','&raquo;', '&raquo;'), $dirty);   

    else
        $dirty=str_replace('&raquo;', '/', $dirty);


    if($debug==true)echo' | after clean: '.$dirty;

    //return
    return $dirty;
}

function makelink($linkme, $debug=false){
    $link=str_replace('&raquo;', '/', $linkme);
    $link=str_replace('//', '/', $link);
    return $link;

}

function recursiveGetSongs($directory, $fileinfo, $useID3, $getID3, $parent=null, $debug, $filtered=null){

    /*
    * configure function here:
    *
    * _usage_
    *   > the disallowed array should include any folders or files you explicitely don't want displayed
    *   > the allowedfiletypes array should include any file extentions you want to play
    */
    $disallowed=array('..', '.', 'simon', 'steve');
    $allowedfiletypes=array('mp3');

    if($filtered!=null){
        $disallowed=array_merge((array)$filtered, (array)$disallowed);
    }

    //simple error fix
    if($directory=='./')
        $directory='.';

    //debug
    if ($debug==true)echo'Dir to open: '.$directory;

    //open directory
    $dir = opendir($directory); 

    while ($read = readdir($dir)) 
    {

        //if ( !in_array($read, $disallowed) AND ( $filter!=null AND in_array($read, $filter) ) )
        if ( !in_array($read, $disallowed) )
        { 
            if($debug==true)echo $read.'<br />';
            //if is not dir, handle file
            if ( !is_dir($directory.'/'.$read) ){

                if($debug==true)echo '^^ not dir | dir: '.$directory.'<br />';

                if( in_array(substr($read, -3, 3), $allowedfiletypes) ){

                    if($useID3==TRUE){

                    //store id3 info
                    $FullFileName = realpath($directory.'/'.$read);
                    if($debug==TRUE)echo'<br />FFN &raquo; '.$FullFileName;
                    $ThisFileInfo = $getID3->analyze($FullFileName);
                    getid3_lib::CopyTagsToComments($ThisFileInfo);
                    $fileinfo[$read]['artist']=$ThisFileInfo['comments_html']['artist'][0];
                    $fileinfo[$read]['title']=$ThisFileInfo['comments_html']['title'][0];
                    $fileinfo[$read]['album']=$ThisFileInfo['comments_html']['album'][0];
                    $fileinfo[$read]['filename']=$ThisFileInfo['filename'];
                    $fileinfo[$read]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));

                    if($debug==true)
                        echo "<br />$read was last modified: " . date ("YmdHis", filemtime($directory.'/'.$read));

                    $fileinfo[$read]['path']=$directory.'/'.$read;
                    if($debug==true)echo'<span style="margin-left: 10px;">path:'.$fileinfo[$read]['path'].' > fn: '.$fileinfo[$read]['filename'].'</span><br /><br />';

                    if($parent!=null)
                        $fileinfo[$read]['from']=str_replace(array('./', '//', '/'), array('', '&raquo;', '&raquo;'), $directory); // was =$parent

                    else
                        $fileinfo[$read]['from']='root'; //testing this

                    if($debug==true)echo'<br />'.$fileinfo[$read]['from'].'<br />';

                    //debug
                    //echo$ThisFileInfo['filename'].'<br />';
                    }
                    else{
                        //store filename
                        $fileinfo[$fileinfo['count']]['path']=$directory.'/'.$read;
                        $fileinfo[$fileinfo['count']]['fn']=$read;
                        if($parent!=null)
                            $fileinfo[$fileinfo['count']]['from']=str_replace(array('./', '//', '/'), array('', '&raquo;', '&raquo;'), $directory);

                        $fileinfo[$fileinfo['count']]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));
                        //$fileinfo[$fileinfo['count']]=date ("YmdHis", filemtime($directory.'/'.$read));
                    }

                    //inc counter
                    $fileinfo['count']=$fileinfo['count']+1; // had ++ and it didn't work
                }
                else
                    ;//do nothing
            }

            //else, must be a folder (as determined above), recurse folder
            else{

                //debug
                if($debug==true)echo '^^ DIR<br />';

                //capture subfolders in case they are needed
                if($parent!='')$fileinfo['folders'].=$parent.'&raquo;'.$read.'|';
                else $fileinfo['folders'].=$read.'|';
                $fileinfo['folderpaths'].=$directory.'/|';

                $fileinfo=recursiveGetSongs($directory.'/'.$read, $fileinfo, $useID3, $getID3, $parent.'/'.$read, $debug, $filtered);

            }

        }

    }
    closedir($dir); 

    return $fileinfo;
}

?>

I am using an array which allows me to prevent particular subfolders from being included in a script that generates an audio playlist. It looks like this:

$disallowed=array('..', '.', 'simon', 'steve');

I would like to find a way of disallowing any folders which are empty, or, perhaps, better still, disallowing folders that do not have any MP3s in them (in case the script interprets folders as not empty if they contain system files which are automatically generated).

I included the full script below so that you can see how the disallowed array fits in.

Would someone be able to help with this?

Thanks,

Nick

<?php

/*
* =====================
* FUNctions
* =====================
*/

/*
* array subvalue sort -- from: http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
*
* this function lets me sort the multidimensional array containing song/artist information by the file modified time, a subvalue
*/
function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    arsort($b); //change this to 'asort' for ascending
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}

/*
* function written to clean up my messy code (too many slashes ... display '/' as '»' (>>) for user friendliness )
*/
function clean($dirty, $type='general', $debug=false){

    //debug
    if($debug==true)
        echo'<br />value before clean: '.$dirty.' (first character: '.substr($dirty, 0, 1).')';

    /*
    * General cleaning -- remove '/' at front and end
    */
    if(substr($dirty, 0, 1)=='/'){
        //echo'<br />found leading /';
        $dirty=substr($dirty, 1, strlen($dirty)-1);
    }

    if(substr($dirty, -1)=='/')
        $dirty=substr($dirty, 0, strlen($dirty)-1);


    //prepare the subfolder display information by type
    if($type=='link')
        $dirty=str_replace(array('//','»'), array('/', '/'), $dirty); 

    else if($type=='display')
        $dirty=str_replace(array('///','//','/'), array('»','»', '»'), $dirty);   

    else
        $dirty=str_replace('»', '/', $dirty);


    if($debug==true)echo' | after clean: '.$dirty;

    //return
    return $dirty;
}

function makelink($linkme, $debug=false){
    $link=str_replace('»', '/', $linkme);
    $link=str_replace('//', '/', $link);
    return $link;

}

function recursiveGetSongs($directory, $fileinfo, $useID3, $getID3, $parent=null, $debug, $filtered=null){

    /*
    * configure function here:
    *
    * _usage_
    *   > the disallowed array should include any folders or files you explicitely don't want displayed
    *   > the allowedfiletypes array should include any file extentions you want to play
    */
    $disallowed=array('..', '.', 'simon', 'steve');
    $allowedfiletypes=array('mp3');

    if($filtered!=null){
        $disallowed=array_merge((array)$filtered, (array)$disallowed);
    }

    //simple error fix
    if($directory=='./')
        $directory='.';

    //debug
    if ($debug==true)echo'Dir to open: '.$directory;

    //open directory
    $dir = opendir($directory); 

    while ($read = readdir($dir)) 
    {

        //if ( !in_array($read, $disallowed) AND ( $filter!=null AND in_array($read, $filter) ) )
        if ( !in_array($read, $disallowed) )
        { 
            if($debug==true)echo $read.'<br />';
            //if is not dir, handle file
            if ( !is_dir($directory.'/'.$read) ){

                if($debug==true)echo '^^ not dir | dir: '.$directory.'<br />';

                if( in_array(substr($read, -3, 3), $allowedfiletypes) ){

                    if($useID3==TRUE){

                    //store id3 info
                    $FullFileName = realpath($directory.'/'.$read);
                    if($debug==TRUE)echo'<br />FFN » '.$FullFileName;
                    $ThisFileInfo = $getID3->analyze($FullFileName);
                    getid3_lib::CopyTagsToComments($ThisFileInfo);
                    $fileinfo[$read]['artist']=$ThisFileInfo['comments_html']['artist'][0];
                    $fileinfo[$read]['title']=$ThisFileInfo['comments_html']['title'][0];
                    $fileinfo[$read]['album']=$ThisFileInfo['comments_html']['album'][0];
                    $fileinfo[$read]['filename']=$ThisFileInfo['filename'];
                    $fileinfo[$read]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));

                    if($debug==true)
                        echo "<br />$read was last modified: " . date ("YmdHis", filemtime($directory.'/'.$read));

                    $fileinfo[$read]['path']=$directory.'/'.$read;
                    if($debug==true)echo'<span style="margin-left: 10px;">path:'.$fileinfo[$read]['path'].' > fn: '.$fileinfo[$read]['filename'].'</span><br /><br />';

                    if($parent!=null)
                        $fileinfo[$read]['from']=str_replace(array('./', '//', '/'), array('', '»', '»'), $directory); // was =$parent

                    else
                        $fileinfo[$read]['from']='root'; //testing this

                    if($debug==true)echo'<br />'.$fileinfo[$read]['from'].'<br />';

                    //debug
                    //echo$ThisFileInfo['filename'].'<br />';
                    }
                    else{
                        //store filename
                        $fileinfo[$fileinfo['count']]['path']=$directory.'/'.$read;
                        $fileinfo[$fileinfo['count']]['fn']=$read;
                        if($parent!=null)
                            $fileinfo[$fileinfo['count']]['from']=str_replace(array('./', '//', '/'), array('', '»', '»'), $directory);

                        $fileinfo[$fileinfo['count']]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));
                        //$fileinfo[$fileinfo['count']]=date ("YmdHis", filemtime($directory.'/'.$read));
                    }

                    //inc counter
                    $fileinfo['count']=$fileinfo['count']+1; // had ++ and it didn't work
                }
                else
                    ;//do nothing
            }

            //else, must be a folder (as determined above), recurse folder
            else{

                //debug
                if($debug==true)echo '^^ DIR<br />';

                //capture subfolders in case they are needed
                if($parent!='')$fileinfo['folders'].=$parent.'»'.$read.'|';
                else $fileinfo['folders'].=$read.'|';
                $fileinfo['folderpaths'].=$directory.'/|';

                $fileinfo=recursiveGetSongs($directory.'/'.$read, $fileinfo, $useID3, $getID3, $parent.'/'.$read, $debug, $filtered);

            }

        }

    }
    closedir($dir); 

    return $fileinfo;
}

?>

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

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

发布评论

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

评论(2

提赋 2024-11-06 09:06:46

使用 glob('somefolder/*.mp3') 测试文件夹中是否有 mp3。该函数将返回一个数组,因此如果它为空,则该文件夹不包含任何 mp3 文件。

Use glob('somefolder/*.mp3') to test if there are mp3s in a folder. The function will return an array, so if it's empty the folder does not contain any mp3 files.

近箐 2024-11-06 09:06:46

您可以尝试 glob() 因为它很灵活,并且比构建“有效数组”更安全。

foreach (glob($dir . "/*.mp3") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

You could try glob() since it's flexible and a little bit more secure than building up a 'valid array'.

foreach (glob($dir . "/*.mp3") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文