我有一個目錄包含子目錄,其中包含一系列的文件。我正在尋找一個腳本,它將查看子目錄和裏面的隨機返回指定數量的文件。多目錄子目錄隨機包括
有幾個腳本可以搜索單個目錄(而不是子文件夾)以及其他可以搜索子文件夾但只返回一個文件的腳本。
爲了說明情況,返回的文件將作爲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/
感謝Quickshiftin,對不起我應該提到我有沒有PHP技能。有沒有可以讓我指點的實例?附: - 隨機文件是鏈接到我的投資組合中的設計項目,而不是廣告客戶:-) – sixtillnine 2012-02-11 10:30:18
我在那裏扔了一些代碼,並帶有評論,希望它有幫助。 – quickshiftin 2012-02-11 10:46:02