...

View Full Version : Pulling from a directory



thedarkonecion
12-12-2007, 04:13 PM
I'm wanting to be able to look into a certain directory and pull whatever is in there and make it a link on the page so the person that is vewing it can pull up what is in that directory. All that is going to be in the directory is going to be documents, and I don't want to have to edit my html document every time there is a new file put into the directory. Is there a way to do this in HTML?

jcdevelopment
12-12-2007, 04:27 PM
do you have an example, like maybe from an existing web page?

ahallicks
12-12-2007, 04:27 PM
No, but there is in php:



<?php
//write to files before loading them

//needs adding

//function creation
function DirList($dir,$align) {
echo "<table align='$align'>";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$link = $dir."/".$file;
$i++;
if(is_dir($dir."/".$file)) {
echo "<tr><td id='dirlist'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Directory '$link'</b></td></tr>";
echo "<tr><td id='dirlist'>"; DirList($link,$align); echo "</td></tr>";
continue;
} else {
echo "<tr><td align='left' id='file'>&nbsp;&nbsp;&nbsp;<a href=\"$link\">$link</a> - ".filesize($link)." bytes.</td></tr>";
}
}
}
if($i <= 0) {
echo "<tr><td align='left' id='file'>&nbsp;&nbsp;&nbsp;-- Folder is empty --</td></tr>";
}
closedir($handle);
}
echo "</table>";
}

DirList("pathtodirectory","left");

?>


The formatting is realyl bad and I'd like to add that I didn't write it and haven't had a play with semantics on it yet... but it basically prints out links to all of the files in a certainly directory.

You will need a server that runs php to do this tho and have a .php extension on the file

bazz
12-12-2007, 06:49 PM
yeh, you'll need a server side language for that.

heres how to do it more quickly in perl :p




my $Filepath = "location of your directory";

opendir($dir,$Filepath);
while(my $files=readdir($dir)){
next unless -d $Filepath.$files && $files!~/^\.+$/;
push(@fileNamesToShow,$files);
}
closedir($dir);
}
print qq(
<ul>
);

foreach $itemInMenu (sort @fileNamesToShow) {
print qq(
<li>$itemInMenu</li>
);
}
print qq(
</ul>
);


Now if you add new files to your directory, they will show in a list, automatically.

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum