2012-02-11 143 views
0

我有一個目錄包含子目錄,其中包含一系列的文件。我正在尋找一個腳本,它將查看子目錄裏面的隨機返回指定數量的文件。多目錄子目錄隨機包括

有幾個腳本可以搜索單個目錄(而不是子文件夾)以及其他可以搜索子文件夾但只返回一個文件的腳本。

爲了說明情況,返回的文件將作爲li包含在旋轉橫幅中。

在此先感謝您的幫助,希望這是可能的。

我想我到了那裏,不正是什麼我設定的目標,但工作不夠好,可以說是更好地爲目的,我使用下面的函數:

<?php function RandomFile($folder='', $extensions='.*'){ 
    // fix path: 
    $folder = trim($folder); 
    $folder = ($folder == '') ? './' : $folder; 

    // check folder: 
    if (!is_dir($folder)){ die('invalid folder given!'); } 

    // create files array 
    $files = array(); 

    // open directory 
    if ($dir = @opendir($folder)){ 

     // go trough all files: 
     while($file = readdir($dir)){ 

      if (!preg_match('/^\.+$/', $file) and 
       preg_match('/\.('.$extensions.')$/', $file)){ 

       // feed the array: 
       $files[] = $file;     
      }    
     }   
     // close directory 
     closedir($dir);  
    } 
    else { 
     die('Could not open the folder "'.$folder.'"'); 
    } 

    if (count($files) == 0){ 
     die('No files where found :-('); 
    } 

    // seed random function: 
    mt_srand((double)microtime()*1000000); 

    // get an random index: 
    $rand = mt_rand(0, count($files)-1); 

    // check again: 
    if (!isset($files[$rand])){ 
     die('Array index was not found! very strange!'); 
    } 

    // return the random file: 
    return $folder . "/" . $files[$rand]; 

} 


$random1 = RandomFile('project-banners/website-design'); 
while (!$random2 || $random2 == $random1) { 
    $random2 = RandomFile('project-banners/logo-design'); 
} 
while (!$random3 || $random3 == $random1 || $random3 == $random2) { 
    $random3 = RandomFile('project-banners/design-for-print'); 
} 
?> 

而且呼應結果到容器(在這種情況下,UL):

<?php include($random1) ;?> 
<?php include($random2) ;?> 
<?php include($random3) ;?> 

感謝quickshiftin對他的幫助,但它是我上面的技能水平一點點。

有關信息,我改變了原來的劇本在這裏找到:

http://randaclay.com/tips-tools/multiple-random-image-php-script/

回答

0

擦洗文件系統的每一次隨機選擇要顯示的文件將是非常緩慢的。您應該提前索引目錄結構。你可以用很多方法做到這一點,嘗試一個簡單的find command或者如果你真的想用PHP,我最喜歡的選擇是RecursiveDirectoryIteratorRecursiveIteratorIterator

將所有結果放到一個文件中,並在您選擇要顯示的文件時從中讀取。您可以使用行號作爲索引,並使用rand函數來選擇一行並因此顯示一個文件。您可能要考慮,雖然東西比蘭特分佈比較均勻,你知道要保持快樂的廣告:)

編輯:

添加一個簡單真實的例子:

// define the location of the portfolio directory 
define('PORTFOLIO_ROOT', '/Users/quickshiftin/junk-php'); 
// and a place where we'll store the index 
define('FILE_INDEX', '/tmp/porfolio-map.txt'); 

// if the index doesn't exist, build it 
// (this doesn't take into account changes to the portfolio files) 
if(!file_exists(FILE_INDEX)) 
    shell_exec('find ' . PORTFOLIO_ROOT . ' > ' . FILE_INDEX); 

// read the index into memory (very slow but easy way to do this) 
$aIndex = file(FILE_INDEX); 

// randomly select an index 
$iIndex = rand(0, count($aIndex) - 1); 

// spit out the filename 
var_dump(trim($aIndex[$iIndex])); 
+0

感謝Quickshiftin,對不起我應該提到我有沒有PHP技能。有沒有可以讓我指點的實例?附: - 隨機文件是鏈接到我的投資組合中的設計項目,而不是廣告客戶:-) – sixtillnine 2012-02-11 10:30:18

+0

我在那裏扔了一些代碼,並帶有評論,希望它有幫助。 – quickshiftin 2012-02-11 10:46:02