2011-04-04 77 views
1

我正在使用一個數組,它允許我阻止特定的子文件夾被包含在生成音頻播放列表的腳本中。它看起來像這樣:將空的文件夾添加到不允許的數組中

$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; 
} 

?> 
+0

當然可以。我會再看一遍。上次我看,雖然沒有工作的答案!但現在可能還有一兩個接受...... – Nick 2011-04-04 10:35:21

回答

0

使用glob('somefolder/*.mp3')測試,如果有一個文件夾中的MP3。該函數將返回一個數組,所以如果它是空的,該文件夾不包含任何mp3文件。

+0

謝謝,但我不知道我將如何包含這是我的腳本,我現在已經在我原來的帖子中包含了這些腳本。 – Nick 2011-04-04 10:40:41

0

你可以嘗試glob(),因爲它比建立「有效數組」更加靈活和安全一點點。

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

謝謝,但我不知道我將如何包括這是我的腳本,我現在已經包含在我的原始文章中。 – Nick 2011-04-04 10:40:13

+0

@Nick從'$ dir = opendir('...到結尾處刪除整個目錄的閱讀部分。你需要的只是實際的ID3閱讀片段。試試我的片段到一個目錄中,然後你就會知道你得到了什麼。在我的代碼片段中,'$ filename'等於你的'$ read'變量,如果我是對的。 – fabrik 2011-04-04 10:46:25

+0

謝謝。我嘗試刪除從'$ dir = opendir('through到'closedir($ dir);'的所有內容,將你的代碼片段放入它的位置,將'$ filename'改爲'$ read',但是沒有出現文件夾。我對PHP的知識非常有限(其他人寫了大部分我發佈的腳本),所以很可能我顯示了我的無知! – Nick 2011-04-04 10:56:03

相關問題