Results 1 to 18 of 18

Thread: All da Files 'n Stuff, man

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    All da Files 'n Stuff, man

    How can I get all the files in a given directory? I'm bored and want to write a file manager in PHP.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I know it probably has something to do with the readdir() function, but I'm not sure how to use it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Nevermind, got it:

    PHP Code:
    <?php
    if ($handle opendir('/path/to/files')) {
        echo 
    "Directory handle: $handle\n";
        echo 
    "Files:\n";

        while (
    false !== ($file readdir($handle))) { 
            echo 
    "$file\n";
        }

        
    closedir($handle); 
    }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    scoutt
    Guest
    are you sure that works?

    while (false !== ($file = readdir($handle))) {

    look all like errors. what is false in there for? and !== is not right.

    while ($file = readdir($handle)) {


    PHP Code:
    <?php
    if (!$handle opendir('/path/to/files')) {
        echo 
    "Directory handle: $handle\n";
        echo 
    "Files:\n";

        while (
    $file readdir($handle)) { 
            echo 
    "$file\n";
        }

        
    closedir($handle); 
    }else{
    echo 
    "Cannot open $default_dir.";
    ?>
    but I am glad I could help

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Scoutt...I'm disappointed. Straight from the Manual:

    PHP Code:
    // Note that !== did not exist until 4.0.0-RC2
    <?php
    if ($handle opendir('/path/to/files')) {
        echo 
    "Directory handle: $handle\n";
        echo 
    "Files:\n";

        
    /* This is the correct way to loop over the directory. */
        
    while (false !== ($file readdir($handle))) { 
            echo 
    "$file\n";
        }

        
    /* This is the WRONG way to loop over the directory. */
        
    while ($file readdir($handle)) { 
            echo 
    "$file\n";
        }

        
    closedir($handle); 
    }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Also:

    "$a !== $a" - Not identical: TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    scoutt
    Guest
    whoa there cowboy

    I didn't say it was totally wrong. My book has something a little different. I never looked at the manual. I know !== is valid code but why check for identical files if you don't have anything to compare it too.

    edit: well never mind my mindless babbleing. I looked at the manual and found out that my book is waaaaayyyyyyyyy out of date. time to buy a new one. it must be pre php4.0

    carry on

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    haha. I haven't had a chance to test this code. Do you think it'll get directories too, or just files?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    it will get both.

    You will need to use is_file() or is_dir() to distinguish between the two.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  10. #10

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks, Brother Mark!
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Originally posted by The Hobo
    Thanks, Brother Mark!
    Who is Mark??
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  12. #12

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I don't know
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    scoutt
    Guest
    Originally posted by cpradio
    it will get both.

    You will need to use is_file() or is_dir() to distinguish between the two.
    if it does anything like the one in my book, which is almost the exact code, it will list the extensions as well.

  14. #14

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    yes that is true, when I said "distinguish" i meant that if he wanted to list all directories and sort them in alphabetical order ontop of all the file he would have to be able to figure out which ones are directories and which are files based on those php functions.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  16. #16
    scoutt
    Guest
    Originally posted by The Hobo
    Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
    I don't think so. you will have to make one, or if you had a friend he could give you one

  17. #17
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Ya you will have to make your own. the filetype() function only returns one of the following: "fifo","char","dir","block","link","file","unknown"

    What I would personally do is grab the extension of the file and run it across an array or table I have built determining what kind of document it is.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  18. #18

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Gotcha. Thanks.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width