Forum Moderators: coopster

Message Too Old, No Replies

List contents of a folder, make list of links

how to do this with simple PHP?

         

Darkelve

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

10+ Year Member



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.

Scally_Ally

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

10+ Year Member



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

dreamcatcher

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

WebmasterWorld Senior Member 10+ Year Member



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