code not working
Page 1 of 1
code not working
Hi allI had this code of displaying random image but it shows error.
Please check and help to rectify.
<html>
<body>
<?php
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
$root = '';
// If images not in sub directory of current directory specify root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
<img src="<?php echo $path . $img ?>" alt="" />
</body>
</html>
Re: code not working
I run the code and it doesn't show any error at all, its completely valid code. Can you please tell me what type of error you are getting so that i can help you? Also is the path for images folder right? check that carefully and set path according to your own folder.Re: code not working
atik wrote:I run the code and it doesn't show any error at all, its completely valid code. Can you please tell me what type of error you are getting so that i can help you? Also is the path for images folder right? check that carefully and set path according to your own folder.
Hi
Thanks for reply.Actually its my mistake, first wrong path given by me and second, spell wrong at one place.I correct it.
Can we make a change to display image after a time interval.
This will only change the image when page get refreshed.
Re: code not working
hshourie wrote:atik wrote:I run the code and it doesn't show any error at all, its completely valid code. Can you please tell me what type of error you are getting so that i can help you? Also is the path for images folder right? check that carefully and set path according to your own folder.
Hi
Thanks for reply.Actually its my mistake, first wrong path given by me and second, spell wrong at one place.I correct it.
Can we make a change to display image after a time interval.
This will only change the image when page get refreshed.
Yes, you can, in that case you have to use ajax request. You can do that using jQuery with php.
Page 1 of 1