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.
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]}>";
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?