Page 1 of 1
[S] Reading contents of a directory inside another directory
Posted: Wed Nov 09, 2011 7:17 am
by social_experiment
I have a script that loops through a directory, when it finds a directory called 'gallery' it loops through that to find any other directories. The problem is that the script reports back that none of the directories within gallery are directories. Any ideas?
Code: Select all
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//echo "$file\n";
if (is_dir($file)) {
if ($file == 'gallery') {
$dirName = $file;
if (is_dir($dirName)) {
$handle = opendir($dirName);
while (false !== ($data = readdir($handle))) {
if ($data != '.' && $data != '..') {
if (is_dir($data)) {
echo $data . ' is a dir <br />';
}
else {
echo $data . ' is not a dir <br />';
}
}
}
}
}
//
}
}
}
}
closedir($handle);
/*
RESULTS
----------
beauty and portraits is not a dir
fashion is not a dir
fine art is not a dir
food is not a dir
love is not a dir
music is not a dir
places and faces is not a dir
*/
?>
Re: Reading contents of a directory inside another directory
Posted: Wed Nov 09, 2011 7:33 am
by twinedev
You need to give it the directory as well, such as:
(and to verify it, put a directory at the same level as "gallery" named the same a directory inside gallery, and your code as you have it would then report that as a directory)
-Greg
Re: Reading contents of a directory inside another directory
Posted: Wed Nov 09, 2011 7:41 am
by social_experiment
Thanks for the help it works perfectly now.

Re: Reading contents of a directory inside another directory
Posted: Wed Nov 09, 2011 7:42 am
by Eric!
EDIT: greg beat me to it...but anyway:
Based on the result output not having a relative path including gallery in $data, I would guess that is_dir() is trying to find the file from your current working directory. So you need something to tell is_dir() where to find $data starting from your working directory.
Here's a function I put together using your opendir/readdir method that can also sort results by regex patterns and organizes the file list with natcasesort. It returns filenames and directories. But if you notice it keeps track of the relative paths starting from the $root as it descends sub-directories. I like this function because it doesn't use glob (which doesn't work well on windows) and it isn't recursive so it takes less memory if you run it on a massive directory structure.
Code: Select all
/**
* Function takes a path $root and dives to $depth within it finding all
* files and directories and returns them in a sorted array
* @param <type> $root root directory
* @param <type> $depth depth to search 0, current directory only (-1 no limit)
* @param <string> $pattern regex pattern to filter file list
* @return array
*/
function read_dir_files($root = '.', $depth=0, $pattern=NULL) {
$files = array();
$directories = array();
$result = array();
$last_letter = $root[strlen($root) - 1];
$root = ($last_letter == '\\' || $last_letter == '/') ? $root : $root . DIRECTORY_SEPARATOR;
$directories[] = $root;
$count = 0;
while (sizeof($directories)) {
$dir = array_pop($directories);
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$file = $dir . $file;
if (is_dir($file)) {
$directory_path = $file . DIRECTORY_SEPARATOR;
if (dir_depth($root, $directory_path) <= $depth || $depth = -1) {
array_push($directories, $directory_path);
}
}
$files[] = $file; // adds directories and files together
}
closedir($handle);
}
}
if (!is_null($pattern)) { // filter for pattern
foreach ($files as $file) {
if (preg_match($pattern, $file) == 1) {
$result[] = $file;
}
}
$files = $result;
}
unset($result);
natcasesort($files);
return $files;
}
Re: [S] Reading contents of a directory inside another direc
Posted: Thu Nov 10, 2011 5:09 am
by social_experiment
Thought of something just now: in my script i use opendir() twice but only close the first resource at the end of the script; Should i close the second directory resource too?
Re: [S] Reading contents of a directory inside another direc
Posted: Thu Nov 10, 2011 5:30 am
by twinedev
Well you only have one you can close, as when you opened the second one, you assigned the resource back to the same variable as the first one. So you are closing the second one at the end of the end of the code, the first one you have nothing that contains the resource id to close it with.
No biggie, they auto close when the script stops, so since you had nothing after them, nothing hurt. Also, at the end of the following IF statement:
if ($file == 'gallery') {
before you close the block, you can do a
break; for better programming practice (assuming you fix the issue with using $handle for both opendirs) since you already matched what you were looking for, no need to keep going. However, as it is coded, it stops anyhow, since your FIRST while loop is using $handle which your second loop had already gone through everything, so it drops out.
If it is just a case of looking for 'gallery' in the given directory, why not just do:
Code: Select all
<?php
if (is_dir('./gallery')) {
$handle = opendir('./gallery');
while (false !== ($data = readdir($handle))) {
if ($data!='.' && $data!='..') {
if (is_dir('./gallery/'.$data)) {
echo $data . ' is a dir <br />';
}
else {
echo $data . ' is not a dir <br />';
}
}
}
}
closedir($handle);
Re: [S] Reading contents of a directory inside another direc
Posted: Thu Nov 10, 2011 6:06 am
by social_experiment
Thanks again for the feedback. The script has evolved and i have assigned a new variable to each instance where opendir() is called. The example i posted at first was a basic version that i've since refactored; the goal is to find any image files within the sub-directories (gallery/music, gallery/food, etc) and select one of those images to be used in a scrolling menu