ShaolinF Posted July 29, 2007 Share Posted July 29, 2007 Hi Guys, My code will randomly select a image in a folder. The files have to be labeled as 1.jpg 2.jpg 3.jpg otherwise it wont work. I want to change this, I want to beable to uses images with different names e.g. test.jpg, image2.jpg, border.jpg etc - How can I do this ? <?php // Change this to the total number of images in the folder $total = "3"; // Change to the type of files to use eg. .jpg or .gif $file_type = ".jpg"; // Change to the location of the folder containing the images $image_folder = "images"; // You do not need to edit below this line $start = "1"; $random = mt_rand($start, $total); $image_name = $random . $file_type; echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"; ?> Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 29, 2007 Share Posted July 29, 2007 You could do it like this... <?php $files = glob("/path/to/my/images/*.jpg"); $path = $files[rand(0,count($files)-1)]; echo "<img src='$path' title='an image' alt='an image' border='0px' />"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.