Click to See Complete Forum and Search --> : List files names(with links) in the same dir


iota
03-08-2006, 11:00 PM
Hi Php Masters,

May I know the php code that lists the files names (together with links) in the same directory ?

For example, let's say, there are five files in "mydocs" dir.


Files : doc1.doc
doc2.doc
doc3.doc
doc4.doc
doc5.doc

Location : http://localhost/mydocs/


I'd like the php code to produce the HTML output like :

<a href="doc1.doc">doc1.doc</a> <br />
<a href="doc2.doc">doc2.doc</a> <br />
<a href="doc3.doc">doc3.doc</a> <br />
<a href="doc4.doc">doc4.doc</a> <br />
<a href="doc5.doc">doc5.doc</a>

Is it possible to implement in PHP ?

Thank you extremely much for your masterpiece.

NogDog
03-08-2006, 11:10 PM
echo "<ul>\n";
foreach(glob("*.*") as $file)
{
echo "<li><a href='$file'>$file</a></li>\n";
}
echo "</ul>\n";

rch10007
03-08-2006, 11:11 PM
$folder = "path_to/docs/";

$handle = opendir ( $folder );

while ( $file = readdir ( $handle ) ) {

$ext = substr ( $file, -4 );

if ( $ext == ".doc" ) {
echo "<a href='$folder$file'>$file</a> <br />\n";
}
}