CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Random File Generator (http://www.codingforums.com/showthread.php?t=239080)

jewradly 09-26-2011 03:36 AM

Random File Generator
 
Yeah hey guys I am new here and fairly new to php. I was wondering if someone could give me a hand to write a code. Basically what I want to be able to do is when the website user clicks on a link, it generates a random image on a separate page. This random image would just be pulled from a folder with a bunch of images. Any help would be great.

Thanks! Austin

ASTP001 09-26-2011 04:04 AM

you can get an array of all the images in that folder by calling the function scandir and then count the number of arrays in it and call rand function to make it randomly pick...

PHP Code:

$images scandir('/images');
$img_num count($images);
$rand rand(2,$img_num); //min is 2 because scandir includes current directory and the directory above
echo "<img src={$images[$rand]}>"


jewradly 09-26-2011 05:31 PM

thanks a lot. I just have one problem.
When I click the link to generate the random image, it works but it shows the image as "broken" everytime. When i copy paste the image url into a new tab, the url shows it pulling from the main directory instead of the folder where I generated it from. Any ideas?

jewradly 09-26-2011 05:33 PM

OH and I know it has something to do with the

<img src={$images[$rand]}> part. I just don't know what to change it to.


All times are GMT +1. The time now is 04:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.