Generating a file list - Post ID 73981

User 355448 Photo


Ambassador
3,144 posts

I have seen pages that create a file list based on some input by the site visitor.

What I want to do is have some code that will create a list of files in a specific folder. Then I want to be able to link to the files to read/play/download the files. As an example, a page that will list every .mp3 file in a mp3 folder, and another page that will produce a list of pdf files in a pdf folder. From these lists, the page would generate a list of links to those files.

I would guess that PHP would be the way, but I have not done enough with PHP to even know where to start.

Any assistance is welcome.
User 2733 Photo


Ambassador
426 posts

I can help with this Bill. Gimme a few hours. I was working on something similar but it can be tweaked. Hang on.
Let's not get all hurt.
User 499732 Photo


Registered User
2 posts

If you haven't gotten a solution for this yet, you could use scandir() if your server is using php 5+ ... for example,


<ul>
<?php
$dir = '/your_directory_location';
$files = scandir($dir);
foreach($files as $file){
?>
<li><a href="<?php echo $dir."/".$file;?>"><?php echo $file;?></li>
<?php
}
?>
</ul>


If you your site is using a php version prior to 5, you can use the opendir, readdir & closedir functions... for example,


$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)


This last will make a clickable list of the files in the particular folder...

best,

Jan
User 355448 Photo


Ambassador
3,144 posts

Jan,

Thanks. I will check these this weekend.

I have too much classwork due in the next couple of days to allow time to test things.

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.