Page 1 of 1

[SOLVED] how to separate files from folders?

Posted: Thu Sep 23, 2004 11:48 am
by paquin1
Ok, I have this code that right now displays the content of a folder that has both files and more folders; it also shows an array for the entire content of the folder. What I want is to add to my array only the folders and not the files.

I added a line 9: if (is_dir($file)) to select only the folders, but it only adds the '.' and '..' folders if I take line 10 out. Also, I thought of using 'filetype' but was not sure on how to only get the dir part out of it.

Can someone help and explain why it does not add other folders? or is there any other way select only the folders?

here is the code:

Code: Select all

<?php
clearstatcache();
$path = "/exports/active/projects/Virtual Bulletin Board/test folder/";
$folders_matched = array();

if (is_dir($path)) {
   if ($dh = opendir($path)) {
     while(($file = readdir($dh)) !== false) {
		//if (is_dir($file)){ //will only give me . and .. as dirs but not the rest of the folders
		if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
		echo "filename: $file : filetype: " . filetype($path . $file) . "<br>";
		array_push($folders_matched, $file);
		//} // uncomment this }
	   	}// end while
   } // end if
   closedir($dh);
}// end if
sort($folders_matched);
reset($folders_matched);

print'<pre>' . print_r($folders_matched, TRUE) . '</pre>';
?>

Posted: Thu Sep 23, 2004 12:00 pm
by timvw
[php_man]filesystem[/php_man]

Code: Select all

function directoriesInDirectory($directory) {
  $results = array();
  if ($handler = @opendir($directory)) {
    while ($file = readdir($handler)) {
      if (is_dir($directory.'/'.$file) && $file != '.' && $file != '..') {
        $results[] = $file;
      }
    }
    closedir($handler);
  }
  return $results;
}

Posted: Thu Sep 23, 2004 12:19 pm
by paquin1
thanks timvw, I actually used your code to figure out the line 9 from another reply you did with the similar code, but the problem was that I didn't understand $directory or at least I was not sure how to turn my code into a function like you have here, let me work on this a little bit and see what I can get. Also, let me read the filesystem that you suggested. But I might come back to ask you if you can explain some things for me.

Posted: Thu Sep 23, 2004 1:52 pm
by timvw
the difference:

is_dir($file) VS is_dir($path . '/' . $file)

Posted: Thu Sep 23, 2004 1:57 pm
by paquin1
Thanks again timvw, this is a great function.

I actually learn couple of things: one was how to make functions, use them, and understand them better, also merging if statements, and last that I need to include the path again for my folder to be seeing as folders.

I'll add both my new working code and the code you made for those people that are looking to learn from looking at different codes. obviously your code is a lot more compact and more efficient.

My new code working:

Code: Select all

<?php
clearstatcache();
$path = "/exports/active/projects/Virtual Bulletin Board/test folder/";
$folders_matched = array();

if (is_dir($path)) {
   if ($dh = opendir($path)) {
     while(($file = readdir($dh)) !== false) {
		if (is_dir($path . $file)){ // add "/" between $path and $file if your path on top does not end with /
		if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
			$folders_matched[] = $file;
		} 
	   	}// end while
   } // end if
   closedir($dh);
}// end if
sort($folders_matched);
reset($folders_matched);

print'<pre>' . print_r($folders_matched, TRUE) . '</pre>';
?>
timvw code used with my code.... much better :D

Code: Select all

<?php
clearstatcache();
$path = "/exports/active/projects/Virtual Bulletin Board/test folder";

function directoriesInDirectory($directory) {
  $folders_matched = array();
  if ($dh = @opendir($directory)) {
    while ($file = readdir($dh)) {
      if (is_dir($directory.'/'.$file) && $file != '.' && $file != '..') {  // in $directory.'/'.$file, '/' is used because path does not end with /
        $folders_matched[] = $file;
      }
    }
    closedir($dh);
  }
  sort($folders_matched);
  reset($folders_matched);
  return $folders_matched;
}
print'<pre>' . print_r (directoriesInDirectory($path), TRUE) . '</pre>';
?>
also you can use this functions to search for files by changing is_dir to is_file and remove the part for . and ..

Posted: Thu Sep 23, 2004 2:02 pm
by timvw
usually i do it like this:

if (is_dir(..)) { add_directory; }
else { add_file; }


And i have a extra variable in the function method
function getcontents ($path, $recursive = false)

Meaby time to lookup what recursive functions are ;)

Posted: Thu Sep 23, 2004 2:05 pm
by paquin1
even better... I think I'm ready to change the subject to [SOLVED], thanks..