首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有子目录的目录中的多个随机包含

带有子目录的目录中的多个随机包含
EN

Stack Overflow用户
提问于 2012-02-11 09:25:34
回答 1查看 416关注 0票数 0

我有一个包含子目录的目录,每个子目录都包含一系列文件。我正在寻找一个脚本,它将在子目录中查找并随机返回指定数量的文件。

有几个脚本可以搜索单个目录(不是子文件夹),还有一些脚本可以搜索子文件夹,但只能返回一个文件。

为了在这种情况下提供一些上下文,返回的文件将以李的形式包含在一个旋转的横幅中。

提前感谢您的帮助,希望这是可能的。

我想我已经做到了,这并不是我想要达到的目标,但工作得足够好,可以说是为了达到这个目的,我使用的是以下功能:

代码语言:javascript
复制
<?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):

代码语言:javascript
复制
<?php include($random1) ;?>
<?php include($random2) ;?>
<?php include($random3) ;?>

谢谢他的帮助,不过这比我的技术水平要高一点。

有关我更改的原始脚本的信息,请访问:

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

EN

回答 1

Stack Overflow用户

发布于 2012-02-11 09:41:43

每一次清理文件系统以随机选择要显示的文件将非常缓慢。您应该提前索引目录结构。您可以使用多种方法,尝试一个简单的查找命令,或者如果您真的想使用PHP,我最喜欢的选择是RecursiveDirectoryIterator + RecursiveIteratorIterator

将所有结果放入一个文件中,然后在选择要显示的文件时从其中读取。您可以使用行号作为索引,并使用兰德函数选择一行,从而选择要显示的文件。你可能想要考虑一些比兰德更均匀的东西,不过,你知道要让广告商高兴:)

编辑:

添加一个简单的真实世界示例:

代码语言:javascript
复制
// 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9239125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档