Recently i needed to list some files in a directory on my server to generate a few things. As i was busy with that i thought some people might need to do the same, but don't really know how or are struggling with it.
That's why i decided to put it on a page here so that everyone can use it. Feel free to play around with it. It isn't a lot of code and simply lists a list of files without any format, but it's a great start if you want to make a list of images or a gallery. Or maybe you want to list rar files, who knows right.
Anyway! Look below for the simple solution to listing files in a folder on your server with PHP
On the top of your page BEFORE everything else you put the following code. Add your folder name between the " " The . between them is the current folder your php page will be in:
<?php
$dirname = ".";
$dir = opendir($dirname);
?>
In your body you put this:
<?php
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo("$file <br />");
}
}
?>
And to make them into links you could use this one instead:
<?php
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo("<a href='$dirname$file'>$file</a> <br />");
}
}
?>
This will list all the files in the appointed folder under each other with a link to that file. You can further edit it and add more functions etc etc to make it more complicated. Do leave a comment if you added something so that we can add it to the page as an example by users or php pros.
So leave a comment |