Welcome to WebmasterWorld Guest from 35.173.234.237

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
posts:115
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:
<ul>
<li><a href="filelocation1">filename 1</li>
<li><a href="filelocation2">filename 1</li>
<li><a href="filelocation3">filename 1</li>
</ul>

etc.?

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

TIA.

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

Full Member

10+ Year Member

joined:Aug 3, 2005
posts:269
votes: 0


you could use scan dir
[uk.php.net...]

and do something like
//------------

<ul>
<?php
$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>
<?php
}
?>
</ul>
//-----------

I aint tested it so may need some tweaking.
Ally

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
posts:3719
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>';

closedir($dir);

dc

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members