View Full Version : determine if directory content is file or folder?

06-04-2005, 08:15 PM
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:
// 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?

Kid Charming
06-04-2005, 08:37 PM
Run each directory object through the is_file() (http://us2.php.net/manual/en/function.is-file.php) function before adding it to the dropdown.

06-05-2005, 04:14 AM
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!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum