Files and Directories
|
|
|
|
<?
class filelist {
var $files;
// the class uses extensions like ("jpg","gif","png")
// the function prepends the document root, so
// give directory information like "/images/album"
function GetList($directory,$ext = FALSE) {
$files = array();
$handle = opendir($_SERVER ['DOCUMENT_ROOT'].$directory);
while(false !== ($file = readdir($handle))) {
//GET THE FILES ACCORDING TO THE EXTENSIONS ON THE ARRAY
for ($i = 0; $i < count($ext); $i++) {
if (eregi(".". $ext[$i] ."$", $file)) {
$files[] = $file;
}
}
} closedir($handle);
$this->files = $files;
}
function WriteList($dir,$ext = FALSE) {
$this -> GetList($dir,$ext);
foreach ($this->files as $element) {
echo "$elementn";
}
}
function WriteImages($dir,$ext = FALSE) {
$this -> GetList($dir,$ext);
shuffle($this->files);
for ($i = 0; $i < 4; $i++) {
$element = $this->files[$i];
echo "<td><img src="$dir/$element" alt="Photo Album" height=147 border=0></td>n";
}
}
} ?>
|
|
|
Usage Example
|
if (file_exists($_SERVER ['DOCUMENT_ROOT']."/global/classes/getfilelist.php")) {
require_once($_SERVER ['DOCUMENT_ROOT'].'/global/classes/getfilelist.php');
$exts = array("jpg","gif");
$photo_dir = "/images/photoalbum/";
$list = new filelist();
$list->WriteImages($photo_dir,$exts);
}
|
|
|
Rate This Script
|
|
|
|