Php Zufallsbild
Beim Seiten-Reload ein Zufallsbild mit PHP anzeigen lassen
Kommentar abgeben zu diesen Beitrag/Code ?Dann hier klicken
Der hier verwendete Code
<?php
function getFiles($dir)
{
static $files;
$allowed_file_extensions = array();
$allowed_file_extensions[] = 'gif';
$allowed_file_extensions[] = 'bmp';
$allowed_file_extensions[] = 'jpg';
$allowed_file_extensions[] = 'jpeg';
$allowed_file_extensions[] = 'png';
$allowed_file_extensions[] = 'GIF';
$allowed_file_extensions[] = 'BMP';
$allowed_file_extensions[] = 'JPG';
$allowed_file_extensions[] = 'JPEG';
$allowed_file_extensions[] = 'PNG';
$len = strlen($dir);
if($dir{$len-1} == '/')
$dir = substr($dir, 0, $len-1);
if(!is_dir($dir))
return false;
$open = @opendir($dir);
if(!$open)
return false;
while($file = readdir($open)) {
if($file == "." || $file == "..")
continue;
if(is_dir($dir.'/'.$file)) {
getFiles($dir.'/'.$file);
}
$e = explode('.', $file);
$ext = $e[ count($e)-1 ];
if( is_file($dir.'/'.$file) && in_array($ext, $allowed_file_extensions) ) {
$files[] = $dir.'/'.$file;
}
}
return $files;
}
$files = getFiles('image/');
$rand = mt_rand(0, count($files)-1);
$image = $files[$rand];
list($files[$rand],$rest) = explode('/Bilder', $files[$rand]);
echo "<a href='$image' title='$files[$rand]'><img class='image' src='$image' alt='$files[$rand]' width='198px'></a>";
?>