2012-02-11 12 views
0

それぞれに一連のファイルを含むサブディレクトリを含むディレクトリがあります。サブディレクトリの中を見るスクリプトを探していますはランダムに指定された数のファイルを返します。複数のランダムにサブディレクトリを持つディレクトリからインクルードする

1つのディレクトリ(サブフォルダではない)を検索できるスクリプトや、サブフォルダを検索して1つのファイルのみを返すスクリプトがあります。

状況に少しの文脈を付けるために、返されたファイルは回転するバナーに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となります。

すべての結果を1つのファイルに保存し、表示するファイルを選択するとそこから読み取るだけです。行番号をインデックスとして使用し、rand関数を使用して行を選択し、表示するファイルを選択することができます。おかげQuickshiftinは申し訳ありませんが、

// 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

は、単純な実世界の例を追加:あなたはEDIT

:)あなたが幸せに広告主を維持するために知っている、しかしより均等にランドよりも、分散何かを検討する必要があります私はPHPスキルの横にあると述べていたはずです。あなたが私に向けて指すことができる実用的な例はありますか? P.S. - ランダムなファイルは、広告主ではなく、私のポートフォリオのデザインプロジェクトへのリンクです。 – sixtillnine

+0

コメントを付けてコードを投げました。 – quickshiftin

関連する問題