Login | Register | FAQ
Anonymous

code not working

+ Post a reply

4 posts Page 1 of 1


code not working

by hshourie » Thu Jul 12, 2012 1:55 am

Hi all

I 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>
User avatar

hshourie

  • Posts: 11
  • Joined: Mon Feb 13, 2012 3:54 am

Re: code not working

by atik » Thu Jul 12, 2012 3:33 am

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.
User avatar

atik

  • Posts: 471
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: code not working

by hshourie » Fri Jul 13, 2012 4:37 pm

User avatar

hshourie

  • Posts: 11
  • Joined: Mon Feb 13, 2012 3:54 am

Re: code not working

by atik » Fri Jul 13, 2012 4:46 pm

User avatar

atik

  • Posts: 471
  • Joined: Mon Oct 17, 2011 4:55 pm


+ Post a reply

Page 1 of 1