SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you use PHP4 to count # of files in a directory?

    Thanks...
    Ian Chiu
    Editor in Chief
    http://www.usbworkshop.com/

  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)
    $dir = dir("pathtodir");
    while($file = $dir->read()) {
    if (($file != ".") && ($file != "..")) {
    $filelist[] = $file;
    }
    }

    print count($filelist);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...what do I put in pathtodir?

    I want the program to check for the number of files found in http://www.domain.com/hardware/images/samples/$id

    Ian Chiu
    Editor in Chief
    http://www.usbworkshop.com/

  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)
    YOU will need to the actual physical path not the http path but the server path for instance if your dir was locate at /home/www/html/stuff/

    $dir = dir("/home/www/html/stuff/");
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...

    The code given seems to have some problems when the directory doesn't exist. How can I check to see if a directory exist? This way, I won't run into any problems with the codes.

    THanks.
    Ian Chiu
    Editor in Chief
    http://www.usbworkshop.com/

  6. #6
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by usbworkshop
    Thanks...

    The code given seems to have some problems when the directory doesn't exist. How can I check to see if a directory exist? This way, I won't run into any problems with the codes.

    THanks.
    You can do this:

    if(!$dir) {
    echo "Error: cannot read directory"; exit;
    }

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I got it working.
    Ian Chiu
    Editor in Chief
    http://www.usbworkshop.com/

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can use
    $d = "/pat/to/some/dir/";
    if (is_dir($d)) {
    $dir = dir($d);
    while($file = $dir->read()) {
    if (($file != ".") && ($file != "..")) {
    $filelist[] = $file;
    }
    }

    print count($filelist);
    }
    else {
    print "the directory ". $d ." does not exist";
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •