PDA

View Full Version : Directory listing display



Spacejaguar
09-29-2005, 12:42 PM
Hello everyone, before I start describing my problem let me say that i searched the web for a lot of time before i decided to post my question here.
I have a website in wich i must list and link some hundreds of files wich i have sorted in several directories. I can always use the default view of a directory listing that a browser generates when you don't have an html file, but that completly ruins the layout and you loose all acess to menus, etc, (i can't use frames). Is there a way or some script that reads the contents of a directory and displays that information (file name, date etc..) and the links to those files inside a table in a preformated html page? Maybe with options to control font size/color? and sort options by date, alphabeticcaly? If anyone knows such a script, or even a different solution to this problem please let me know. By the way, new files will be continually put on the directories from time to time.
Thanks for reading. :)

Twey
09-29-2005, 03:33 PM
I can always use the default view of a directory listing that a browser generates when you don't have an html file
The browser doesn't generate this, the server does. The easiest way to solve your problem is to edit this template.

Failing this, you would need a server-side script:

<?php
$path = "/var/www/html/files"; // Path from the server's point of view
$browserpath = "/files"; // Path from the browser's point of view - no trailing slash!
$files = "<ul>\n";
$dir = opendir($path);

while($file = readdir($dir)) {
if(($file != ".") && ($file != "..")) {
$files .= "\t<li><a href=\"$browserpath/$file\">$file</a></li>\n";
}
}
closedir($dir);
?>
Files in <?=$browserdir?>:
<?=$files?>

enemeth
09-25-2008, 02:37 AM
Thank you for that code up there it works great for me , but can someone just tell me what i need to put in it to alphabetise the list???

thank you ,

Elaine

Nile
09-25-2008, 02:56 AM
Not tested, no guarantee it will work:

<?php
$path = "/var/www/html/files"; // Path from the server's point of view
$browserpath = "/files"; // Path from the browser's point of view - no trailing slash!
$files = "<ul>\n";
$dir = opendir($path);
$c = 0;
while($file = readdir($dir)) {
if(($file != ".") && ($file != "..")) {
$c += 1;
$filesArrA[$c] = $file;
$filesArrB[$c] = $browserpath/$file;
$filesArrA[$c] .= "\t<li><a href=\"$filesArrB[$c]/$filesArrA[$c]\">$filesArrA[$c]</a></li>\n";
}
}
for($i=0;$i<=$c;$i++){
sort($filesArrA[$i]);
}
closedir($dir);
echo "Files in $browserdir<br />";
for($i=0;$i<=$c;$i++){
echo $filesArrA[$c];
}
?>

enemeth
09-25-2008, 12:29 PM
no it didnt work :(

Nile
09-25-2008, 09:55 PM
Does it show the files, or do you get an error?

Twey
09-26-2008, 03:25 AM
<?php
$path = '/var/www/html/files';
$browserpath = '/files';

$files = scandir($files);
sort($files);
?>
Files in <?php echo $browserpath; ?>:
<ul>
<?php foreach ($files as $file): ?>
<li>
<a href="<?php echo $browserpath; ?>/<?php echo $file; ?>">
<?php echo $file; ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Medyman
09-26-2008, 03:41 AM
http://css-tricks.com/improving-the-default-directory-view/

Twey
09-26-2008, 04:59 PM
Not sure I trust that. 'Different browsers have different takes on it?' Drivel, sir! It's the server that defines how that page appears, nothing more.

Medyman
09-26-2008, 06:25 PM
Not sure I trust that. 'Different browsers have different takes on it?' Drivel, sir! It's the server that defines how that page appears, nothing more.

The guy who wrote that blog post isn't the one who wrote the script. So take that for all it's worth.

I don't make any claims about the script as I don't and have not used it. Just thought it was interesting.