Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile 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

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    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]}>"

  • Users who have thanked ASTP001 for this post:

    jewradly (09-26-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •