Page 1 of 1

Sorting/Displaying the contents of a folder

Posted: Wed Jul 27, 2005 6:53 am
by jleampark
I want to write a simple page which will read the contents of a folder and returns or displays all the .doc or .pdf or .wpd files in the folder -- sorted alphabetically and numerically.

Can anyone help me with the code?

Thanks!

Joe

more info (if it helps)

Posted: Wed Jul 27, 2005 7:09 am
by jleampark
Here it what I have already. It works in that it displays all the documents in the folder but the sorting it off...

My code is:

Code: Select all

$regexp = '\.(doc|wpd|dot)$'; 
// For every additional extension, just add the extension, like exe would be put in like 
// (doc|wpd|exe)
$dir = "../Projectform";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && ereg($regexp, $file)) {
// split the file name in 2 parts (one before and one after the dot)
$fileparts = explode(".",$file);
echo "<a href=\"" . $dir . "/" . $file . "\">" . $fileparts[0] . "</a><br />\n";
}
}
closedir($handle);
}
A portion of my results is:

Code: Select all

20010016- 1997-98_NonEmployerSurvey
20020017-Research_LogEvalSoftware
20050066-M3_IVR
20020018-EconDrillDownApp
20020019-DirectoryInterProbSolvingEnviron
While the names of the files may not make sense to you, why would 20050066 appear to be out of sequence??

Posted: Wed Jul 27, 2005 8:21 am
by dyonak
Glob would work well for what you need. This is untested, let me know if you have problems.

alphabetical results:

Code: Select all

$Results = glob("{*.pdf,*.doc,*.wpd}", GLOB_BRACE);
sort( $Results );
print_r ($Results);
reverse alphabetical results:

Code: Select all

$Results = glob("{*.pdf,*.doc,*.wpd}", GLOB_BRACE);
sort( $Results );
print_r ($Results);

Posted: Wed Jul 27, 2005 9:19 am
by jleampark
I simply cut and pasted your suggestion and got these errors:

Code: Select all

Warning: glob() expects parameter 2 to be long, string given
and

Code: Select all

sort() expects parameter 1 to be array, null given
Am I missing something? (I am a php-dunce.)

Thanks.

Posted: Wed Jul 27, 2005 6:49 pm
by dyonak
It was wrong at first, I tested what's up there now and it successfully returned the array.