-1
我想顯示來自多個derctories的圖像。顯示來自多個目錄的圖像
我有這個主文件夾(背景)和這個DIR裏面我有45個文件夾每個文件夾之間有10-20個圖像。
我想顯示目錄中的所有圖像。
問候 Al3in
我想顯示來自多個derctories的圖像。顯示來自多個目錄的圖像
我有這個主文件夾(背景)和這個DIR裏面我有45個文件夾每個文件夾之間有10-20個圖像。
我想顯示目錄中的所有圖像。
問候 Al3in
試試這個來代替:
<?php
// Recursivly search through a directory and sub-directories for all
// image files. The returned result will be an array will all matches
// and their path (relative to the path sent in through the $dir argument)
//
// $dir - Directory to search through
// $filetypes - Array of file extensions to match
//
// Returns: Array() of files that match the $filetypes filter (or standard
// image file extensions by default).
//
function recursiveFileSearch($dir = '.', $filetypes = null)
{
if (!is_dir($dir))
return Array();
// create a regex filter so we only grab image files
if (is_null($filetypes))
$filetypes = Array('jpg','jpeg','gif','png');
$fileFilter = '/\.('.implode('|',$filetypes).')$/i';
// build a results array
$images = Array();
// open the directory and begin searching
if (($dHandle = opendir($dir)) !== false)
{
// iterate all files
while (($file = readdir($dHandle)) !== false)
{
// we don't want the . or .. directory aliases
if ($file == '.' || $file == '..')
continue;
// compile the path for reference
$path = $dir . DIRECTORY_SEPARATOR . $file;
// is it a directory? if so, append the results
if (is_dir($path))
$results = array_merge($results, recursiveFileSearch($path,$filetypes));
// must be a file, see if it matches our patter and add it if necessary
else if (is_file($path) && preg_match($fileFilter,$file))
$results[] = str_replace(DIRECTORY_SEPARATOR,'/',$path);
}
// close the directory when we're through
closedir($dHandle);
}
// return the outcome
return $results;
}
?>
<html><body><?php array_map(create_function('$i','echo "<img src=\"{$i}\" alt=\"{$i}\" /><br />";'),recursiveFileSearch('backgrounds')); ?></body></html>
什麼是你的問題? – 2010-11-05 15:30:05
這個「背景」頂層目錄中是否有任何圖像,您**不希望顯示? – 2010-11-05 15:34:10