logo       

question about reading directory contents: msg#00201

php.tcphp

Subject: question about reading directory contents

admittedly I am somewhat green when it comes to Unix, but this one has me
utterly baffled. This is running on: php 5, apache 2, suse 9.3 using the
readdir command.

What I've done is create a directory structure like the following
0 - Coversheets
1 - Plans/1 - Title
1 - Plans/2 - Structural
1 - Plans/3 - Mechanical
2 - Specs
3 - Addenda
4 - Other

Inside these directories are any number of files. I created a little
recursive function that starts at a root directory and outputs a directory
name then all the files inside of it if there is at least 1 file. Under
Windows the directories printed in order and the files were alphabetized
as well.

0 - Coversheets
File A
File B

1 - Plans/1-Title
File C

2 - Specs
File D
File E
File F

4 - Other
File G

When I bring this over to linux, a ls or ls-l will list the directories
in the same order but when viewed on the webpage there doesn't appear to
be any reason for the order of the files or directories. I've tried
seeing if it had to do with modified time, size of files / directories,
even the order that they were created in. In the 4 jobs that I created
and then uploaded files with different sizes, in differed order it would
always list the directory heading in the following
specs
coversheets
other
addenda
Plans/Structural
Plans/Title
Plans/Mechanical

Does anyone have an idea what is happening?

Thanks for any ideas

Bob Brazeau

And its not the prettiest code ever but here is what I was using.

// Function that will list the files / subfiles of a directory. Has a
switch to print
// out the file names and links or as radio buttons.

function GetDirContents($dir, $RootPath,$ListType = 0){
//initialize all vars, even if register globals is off
$RootLen = ""; $root = "";
$ct = ""; $Loc = "";
$files[] = ""; $fileName = "";
$file = "";

//The dir and root path are initially the same, dir changes over time,
but root stays the same.
$RootLen = strlen($RootPath);
ini_set("max_execution_time",10);

// in case a file is sent instead of a dir
if (!is_dir($dir)){(" \n No files currently in directory: $dir!
\n");}

// Now that there is a Dir, print out the name once, then begin
listing the files. There is a switch
// here to either print as a link or a radio button. This is a
recursive function and will list all sub folders
if ($root=@opendir($dir)){
$PrintDirOnce = 0;
while ($file=readdir($root)){
if($file=="." || $file==".."){continue;}

if(is_dir($dir."/".$file)){
//subdirectory found, make recursive call and lists
its contents.
GetDirContents($dir."/".$file, $RootPath, $ListType);
}else{
if ($dir !== $RootPath) {
if ($PrintDirOnce == 0) {
$PrintDirOnce = 1;
$Loc = str_replace("/", " - ",substr($dir,$RootLen+1));
$Loc = ereg_replace('[0-9] - ', "", $Loc); // pattern
match to remove # -
echo " \n <P><font
size=\"+1\"><strong>$Loc:</font></strong><br>\n";
}
// print out all files in the Dir, emptying the list first
$files = "";
$files[]=$dir."/".$file;

foreach ($files as $f){
$fileName = strrpos($f,"/");
$fileName = substr($f,$fileName+1);
if ($ListType == 0) { //just list the file, or turn it
into a radio button
echo "\n &nbsp; &nbsp; &nbsp; <A
href=\"$f\">$fileName</a><BR>\n";
} else {
echo " &nbsp; &nbsp;<input type=radio name=\"File\"
value=\"$dir/$fileName\">$fileName<br>\n";
}
}//foreach
}//if ($dir !== $FileRoot)
} // else
} //While
} //if ($root=@opendir($dir))
} //Function



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise