Php 包含子目录的目录中的多个随机包含
我有一个包含子目录的目录,每个子目录包含一系列文件。我正在寻找一个脚本,它将查看子目录并随机返回指定数量的文件 有几个脚本可以搜索单个目录(不是子文件夹),还有一些脚本可以搜索子文件夹,但只返回一个文件 为了让情况有一点背景,返回的文件将作为李的文件包含在一个旋转的横幅中 提前感谢您的帮助,希望这是可能的 我想我已经做到了,这并不完全是我想要实现的,但已经足够好了,可以说更好了,我正在使用以下功能:Php 包含子目录的目录中的多个随机包含,php,Php,我有一个包含子目录的目录,每个子目录包含一系列文件。我正在寻找一个脚本,它将查看子目录并随机返回指定数量的文件 有几个脚本可以搜索单个目录(不是子文件夹),还有一些脚本可以搜索子文件夹,但只返回一个文件 为了让情况有一点背景,返回的文件将作为李的文件包含在一个旋转的横幅中 提前感谢您的帮助,希望这是可能的 我想我已经做到了,这并不完全是我想要实现的,但已经足够好了,可以说更好了,我正在使用以下功能: <?php function RandomFile($folder='', $extens
<?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');
}
?>
每次清理文件系统以随机选择要显示的文件将非常缓慢。您应该提前为目录结构编制索引。您可以通过多种方式实现这一点,尝试一个简单的方法,或者如果您真的想使用PHP,我最喜欢的选择是plus 将所有结果放在一个文件中,并在选择要显示的文件时从其中读取。您可以使用行号作为索引,并使用该函数选择一行,从而选择要显示的文件。你可能想考虑比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]));
谢谢Quickshiftin,很抱歉我应该提到我几乎没有PHP技能。你能给我举一些有用的例子吗?附言——这些随机文件是指向我作品集中设计项目的链接,而不是广告商:-)
// 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]));