SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting number of images in a folder

    Hi, i have a little script on my page here (http://www.mitchellpage.com.au/mitcho/) which shows an image at random everytime the page is refreshed: the script of generating the rand number is as follows:

    Code:
    rnd.today=new Date();
    rnd.seed=rnd.today.getTime();
    
    function rnd() {
            rnd.seed = (rnd.seed*9301+49297) % 233280;
            return rnd.seed/(233280.0);
    };
    
    function rand(number) {
            return Math.ceil(rnd()*number);
    };
    and then the image has this script wrapt around it:

    Code:
    <script language="JavaScript">document.write('<img src="images/center_' + rand(7) + '.jpg" width="526" height="370" alt="Powered by everyday life">');</script>
    Just say i create a new folder called centre_images and inside this put only the images to be shown on this page-Rather than updating this script everytime i add a new centre image to this folder, is there a way of detecting the number of images in that folder and then changing the javascript around the img tag to ="centre_images/center_' + rand(i) + 'jpg" where i is equal to number of images in the centre_images folder?

    Thanks guys!

  2. #2
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript cannot access the file system on either the server or the client. Any information about the files on the server must be passed to JavaScript by server-side scripts. Client-side file information is strictly off limits, for security reasons.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i see,

    how would i go about this then.
    can i write a php script and place it inside this folder which returns the number of images in the folder as an integer?

    So i just created this little php script to determine the number of files in the folder - see this http://www.mitchellpage.com/mitcho/i...mage_count.php

    How can i use this in conjunction with javascript to find achieve what i stated in previous post?

    Thanks!

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use ajax to read your php output and then use JS to split the filenames and input them into an array.

    Also, if you set the browse attribute on the folder, allowing users to view the contents, I am sure you can read that using ajax (xmlhttp) as well without the need to use a seperate script.

    Gav


  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do all the work in php you have no need for a javascript function to figure out the random image, you can use plain html.

    <img src="getRandomImage.php" /> etc


Bookmarks

Posting Permissions

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