SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2001
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie problem with readdir() providing blank file names

    Hello. I recently started learning PHP. One of my first tasks has been to write a simple script that obtains the filenames of several files in a subdirectory and then prints them. (I am building up to a script that shall output the HTML necessary to display a page of thumbnails which link to full-sized photos.) My script's source is as follows:

    PHP Code:
    $imageDir ImageList ("screens/small");

    for (
    $i 0$i count ($imageDir); $i++) {
        
    printf ("%s<BR>\n"$imageDir[i]);
    }

    function 
    ImageList ($sPath) {
        
    //Load Directory Into Array
        
    $imageList = array();
        
    $handle opendir ($sPath);

        while (
    $file readdir($handle)) {
            if (
    $file != "." && $file != "..") {
                
    array_push ($imageList"$file");
            }
        }

        
    //Clean up and sort
        
    closedir ($handle);     
        
    sort ($imageList);
        return 
    $imageList;

    The output from this script is composed only of the <BR> and \n statements; it simply does not output any file names. I shall appreciate any assistance that you can provide. Thanks in advance.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't figure out what was wrong with your code so I just rewrote it a bit and now it should work.

    PHP Code:
    $imageDir ImageList(".");

    foreach(
    $imageDir as $key => $val) {
        print 
    "$val<br>\n";
        }

    function 
    ImageList($sPath) {
        
    $d dir($sPath);
        while (
    $file $d->read()) {
            if ((
    $file != ".") && ($file != "..")) {
                
    $imageList[] = $file;
                }
            }
        
    //Clean up and sort
        
    $d->close();     
        
    sort($imageList);
        return 
    $imageList;
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Member
    Join Date
    Jul 2001
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply. I copied and pasted your code exactly as it appears. When executed, the script generates a "parse error" on line 8 (the line with the "foreach" statement). I tried it on two separate hosts with PHP support (Hobbiton and F2s); one was running PHP 4.0.5, the other 4.0.6RC3. Both generated the same error. I shall appreciate any additional help you may be able to provide. Thanks again.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay you must bot have support for foreach() or something although I though anything newer than 4.0.4 had it. anyways try changing

    PHP Code:
    foreach($imageDir as $key => $val) {
        print 
    "$val<br>\n";
        } 
    to

    PHP Code:
    for ($i=0;$i<count($imageDir);$i++)  {
        print 
    $imageDir[$i]."<br>\n";
        } 

    If this still produces a parse error could you please copy and paste the exact code you are using here.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Incidently I think the reason your first attempt failed, was this line

    PHP Code:
    printf ("%s<BR>\n"$imageDir[i]); 
    Should have been

    PHP Code:
    printf ("%s<BR>\n"$imageDir[$i]); 

    Noctie the misssing $ sign before i in yours.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Member
    Join Date
    Jul 2001
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After changing the foreach statement to a for loop, I continue to get a parse error. This time, it states that there was a parse error on the line that reads as follows: "for*($i=0;$i<count($imageDir);$i++)**{".

    You were correct in pointing the error in my original code. Everything works perfectly now that a '$' precedes the 'i'. Although the code that you provided me with doesn't function correctly (no idea why), the original code is fine, thanks to your pointing out my error. Thanks for all your help.


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
  •