Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

List contents of a folder, make list of links

how to do this with simple PHP?

1:05 pm on Feb 15, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 31, 2004
votes: 0

Does anyone know how I can write a script that:

- reads in all the files in a particular directory
- displays the file names in a html list and makes a link of them:
<li><a href="filelocation1">filename 1</li>
<li><a href="filelocation2">filename 1</li>
<li><a href="filelocation3">filename 1</li>


So basically it creates a list of links with the contents in that directory,
so you can download them from there.


4:05 pm on Feb 15, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 3, 2005
votes: 0

you could use scan dir

and do something like

$dir = '/my_directory_location';
$files = scandir($dir);
foreach($files as $ind_file){
<li><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></li>

I aint tested it so may need some tweaking.

4:25 pm on Feb 15, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
votes: 0

Note that Scally_Ally`s solution would indeed work, but is only compatible with PHP 5. If that doesn`t work, you need the opendir [uk.php.net], readdir [uk.php.net] & closedir [uk.php.net] functions.

$dir = opendir('files/');
echo '<ul>';

while ($read = readdir($dir))

if ($read!='.' && $read!='..')
echo '<li><a href="files/'.$read.'">'.$read.'</a></li>';


echo '</ul>';




Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members