SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    read files from a given folder?

    Hello!

    If someone could put me on the right track i would appreciate.

    Here is what i want to do :

    All images that are in a folder will be listed in a little dropdown menu, if this or that image from that dropdown menu is selected it will be displayed whereever i want to display it.

    So basically, i need to read the content from a folder (ie. img1.gif etc..), have it displayed in a dropdown menu AND whichever image listed in the dropdown menu gets selected will be displayed (whereever i want to).

    I don't need a complete script, i would just need to be put on the right track, ie. how could i check the folder, have it displayed (use a while loop?, if so check for what? while (what?) ) etc..
    There are so many functions available on php.net that i dont know where to start looking.

    Thanks a lot in advance.

    Best regards from Vienna, and have a nice Weekend!
    datune

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it reads all the images in my folder images and puts them into an array in javascript

    PHP Code:

    <?

    print "var url = new Array;\nvar Person = new Array;\nvar imgSize = new Array;\nvar filePath = new Array;\n\n";

    $d dir('images/');
    $i=0;

    while (
    false !== ($entry $d->read())) {
        if (
    $entry != '.' && $entry != '..' && !is_dir('images/'.$entry))
        {
            
    $size  GetImageSize ('images/' $entry);
            
    $file  basename ('images/' $entry);
            
    $fileS filesize ('images/' $entry);

            print     
    "url[$i]        = 'images/$entry';            \n";
            print     
    "Person[$i]     = SelectPeep(10);          \n";
            print 
    "imgSize[$i] = 'Width   = $size[0] & Height = $size[1]';  \n";
            
    $i=$i+1;
        
        }

    }
    $d->close();
    print 
    "\nhowMany=($i - 1);\n"
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Andrew!

    Well first of all thanks, but this is useless to me, since i do not intend to rely on javascript for this to work.(What if a user has Javascript disabled?).

    All i want to achieve is read the image names from a given folder and display them in a pulldown menu to select. That's it.

    I can't seem to get it done.

    Edit:
    Ha, i just found out there is a function called readdir on php.net and i actually found an example of how to do it.
    Now as for education purposes could someone please explain to me why this
    PHP Code:
    if ($file != "." && $file != "..")
    {
        echo (
    "$file\n<br>");

    stripts out the . and .. ?
    Let me tell you how i read this

    If the variable called file IS NOT a dot AND variable file IS NOT dot dot then echo the content of the given folder.
    But the variable file is always a dot and a dot dot, so how come it still actually strips out the . and .. and displays the content of the directory?
    I really don't get this.
    Thanks a lot in advance (i feel pretty stupid for not understanding this and having to ask for an explanation here, but before i pretend i do understand i rather feel stupid and ask people to explain).

    Cheers,
    datune
    Last edited by datune; Apr 8, 2002 at 07:28.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well for a start i didn't intend you used javascript that was the reason I used it for I just simply copied it and pasted the code as you asked to be put on the right track.

    PHP Code:

    if ($file != "." && $file != "..")
    {
        echo (
    "$file\n<br>");

    all this does is check that variable $file does not have a . or .. in it. If there are no .'s in it then it will print variable $file on the page with a line break.

    You will also find that you need to check if the directory actually exists.

    Also the code i gave you does the lot


    PHP Code:

    $d 
    dir('images/');
    $i=0;

    while (
    false !== ($entry $d->read())) {
        if (
    $entry != '.' && $entry != '..' && !is_dir('images/'.$entry))
        {
            
    $size  GetImageSize ('images/' $entry);// this is optional this gets each image size
            
    $file  basename ('images/' $entry);// this is optional this gets the basename for each image
            
    $fileS filesize ('images/' $entry);// this is optional this gets the size for each image

            
    print "$i = $entry\n"//prints image filename
            
    print "Width = $size[0] & Height = $size[1]\n";//prints height and width of each image
            
    $i=$i+1;
        
        }

    }
    $d->close(); 
    this code does not contain any javascript and will print two things which are the image names

    [list=1][*]$d->read() reads the dir('images/'); which is the same as the function you have found readdir()[*]!is_dir checks if the location of the files exists[/list=1]


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
  •