...

View Full Version : Help with an open directory script



beasters
08-28-2007, 09:51 PM
What I'm trying to do is come as close to the Apache open directory page as possible, what i have so far is extremely simple, and lists all files and folders in the directory the php file is in. Here goes...


<?

function getFiles($path) {
//Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
//sorted by the file name (case insensitive). If two files are identical when compared without case, they will sort
//relative to each other in the order presented by readdir()
$files = array();
$fileNames = array();
$i = 0;

if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if ($file == "." || $file == "..") continue;
$fullpath = $path . "/" . $file;
$fkey = strtolower($file);
while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
$a = stat($fullpath);
$files[$fkey]['size'] = $a['size'];
if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
$files[$fkey]['name'] = $file;
$files[$fkey]['type'] = filetype($fullpath);
$fileNames[$i++] = $fkey;
}
closedir($dh);
} else die ("Cannot open directory: $path");
} else die ("Path is not a directory: $path");
sort($fileNames,SORT_STRING);
$sortedFiles = array();
$i = 0;
foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];

return $sortedFiles;
}

$files = getFiles("./");
foreach ($files as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n";
?>

I'd at least like to be able to sort the information by class (file/folder so that folders appear at the top of the list) and create the ability to exclude certain file extensions, such as my *.php and *.html files. This script isn't my work, just something i picked up a while ago, but I decided to try and restart my little project. Any help, even minor improvements on the code would be wonderful, I'm very noobish when it comes to scripting, never could pick up on it in a timely manner...
Thanks for your help!
http://matts-mods.com/files/bond_icon.gif

beasters
08-31-2007, 03:35 AM
Bump

Recap:
I'm looking for the ability to exclude *.php and *.html files, and to show folders before files on the list, and any other little tidbits you think would be an improvement!

Heres what I'm working with www.matts-mods.com/dir.php (http://www.matts-mods.com/dir.php)

Inigoesdr
08-31-2007, 03:50 AM
if ($file == "." || $file == ".." || strtolower(substr(strrchr($file, '.'), 1)) == 'php' || strtolower(substr(strrchr($file, '.'), 1)) == 'html') continue;
As for the other problem make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays.

beasters
08-31-2007, 08:00 PM
As for the other problem make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays.

can someone do this for me? I'm not quite knowledgeable enough :-/

beasters
09-06-2007, 08:14 PM
bump, can anyone "make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays." for me please?

thanks!

Inigoesdr
09-06-2007, 10:44 PM
This is the general idea:

function getFiles($path)
{
//Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
//sorted by the file name (case insensitive). If two files are identical when compared without case, they will sort
//relative to each other in the order presented by readdir()
$files = $fileNames = $dirs = array();
$i = $j = 0;

if (is_dir($path))
{
if ($dh = opendir($path))
{
while (($file = readdir($dh)) !== false)
{
if ($file == "." || $file == ".." || strtolower(substr(strrchr($file, '.'), 1)) == 'php' || strtolower(substr(strrchr($file, '.'), 1)) == 'html') continue;
$fullpath = $path . "/" . $file;
$fkey = strtolower($file);
if(is_dir($fullpath))
{
$dirs[$j++] = $fkey;
}
else
{
while (array_key_exists($fkey,$fileNames))
$fkey .= " ";
$a = stat($fullpath);
$files[$fkey]['size'] = $a['size'];
if ($a['size'] == 0)
$files[$fkey]['sizetext'] = "-";
elseif ($a['size'] > 1024)
$files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
elseif ($a['size'] > 1024*1024)
$files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
else
$files[$fkey]['sizetext'] = $a['size'] . " bytes";
$files[$fkey]['name'] = $file;
$files[$fkey]['type'] = filetype($fullpath);
$fileNames[$i++] = $fkey;
}
}
closedir($dh);
}
else
die ("Cannot open directory: $path");
}
else
die ("Path is not a directory: $path");

sort($fileNames,SORT_STRING);
$sortedFiles = array();
$i = 0;
foreach($fileNames as $f)
$sortedFiles[$i++] = $files[$f];

sort($dirs, SORT_STRING);
return array('dirs' => $dirs, 'files' => $sortedFiles);
}

$files = getFiles("./");
echo 'Dirs:<br />';
foreach ($files['dirs'] as $dir) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$dir\">$dir</a></b><br>\n";
echo '<br /><br />Files:<br />';
foreach ($files['files'] as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n";
You'll have to make changes, since this is only an example.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum