Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    determine if directory content is file or folder?

    Hello! I am working on a small piece of code as part of a larger project, and I have run into a predicament of sorts:

    I started with this code:
    PHP Code:
    // Write File Select Section:
        
    $dir_name ".";
        
    $dh opendir($dir_name) or die("Couldn't open specified directory.");
        echo 
    "<!-- File selections --> \n";
        echo 
    "<form action=\"testpage.php\" method=\"POST\"><select name=\"search_this_file\">";
        while (!((
    $file readdir($dh)) === false ) ) {
            if ((
    $file != ".") && ($file != "..")) {
                echo 
    "<option value=\"".$file."\"";
                global 
    $search_this_file;
                if (
    $file == $search_this_file) {
                    echo 
    " selected";
                    }
                echo 
    ">".$file."</option>";
                }
            }
        echo 
    "</select><input type=\"submit\" value=\"Go\"></form> \n";
        
    closedir($dh); 
    Basically, it reads the contents of the current directory, excludes the "." and ".." written for parent directories, and writes the remaining contents to a select box.

    The problem is, the rest of my code depends on $search_this_file being an actual file, not a folder (if the form is submitted and $search_this_file is a folder, an error occurs). What I need is code that will determine whether each of the contents of the directory is either a file or a folder. Is there an easy way to do this?

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Run each directory object through the is_file() function before adding it to the dropdown.

  • #3
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! Now, after getting more code complete with is_file() and is_dir() per your suggestion, I am running into a problem. I am trying to make a "file tree" of sorts out of a select box, but I'm having a tough time of it.

    I have posted it under a new topic since, well, it is a new topic! Thanks for your help, though. I'm still learning and have a hard time remembering all those pre-defined functions!


  •  

    Posting Permissions

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