2012-02-11 129 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

相关问题