How to create a list of files in a folder

I have searched using Google and searched on this site. It may be that I just don’t have the right terms for my search, but…

I am trying to build a site where various people can login to get stored pdf documents. One or two people will be uploading the pdf and others will routinely login to get/read the files.

I know how to use php to get the authorization level and direct the administrators to an upload page, and have standard users directed to a different page.

Is there a way to get php to build a list of files in a specific folder on the website and display this list?

There sure is, take a look at the DirectoryIterator :slight_smile:

There are examples in the comments on that page.

Another way is to use glob: PHP: glob - Manual

Thanks. I now have something to play with and learn how to use.

I had not thought of searching on iterate and would never have thought of glob.

I am sure I will be back with questions. :slight_smile:

Well, I had the script working, and failed to realize it was locked on only three files. I created an upload form and uploaded a couple of pdf files that do not show up in the list.


<?php
foreach (new DirectoryIterator('../documents') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo '<a href="/documents/' . $fileInfo->getFilename() .  ' ">' . $fileInfo->getFilename() .  '</a><br> ';
}
?>

This worked once, and I do not remember changing this, but is there an error here?

i think this is the problem…

foreach (new DirectoryIterator('../documents') as $fileInfo)

try to instantiate the class to a variable first before placing it in a loop…


$di = new DirectoryIterator('../documents');
foreach ($di as $fileInfo)

and also i think that new DirectoryIterator(‘…/documents’) will not return an array…
maybe you need to execute a method first?


$di = new DirectoryIterator('../documents');
foreach ($di->getSomething() as $fileInfo)

This is embarrassing. :blush:

I had somehow created two document folders in different locations. I have been uploading to the wrong folder, so the problem is my upload form pointing to the wrong directory.

Guess I should be more awake when I am writing code. :slight_smile: