php – How to show all images in a folder?-ThrowExceptions

Exception or error:

I’m trying to make a page where I can share pictures. For the time being, I’m not using a database, just uploading them into my htdocs. All of my pictures will be PNG, and saved in a folder called myimages. I want a page to list all of these images, if it’s possible.
Thanks for the help!

How to solve:

If you want to list all files in a directory, you can use scandir:

$dir = '/your/folder';
$files1 = scandir($dir); //returns an array of file names (strings)

However, if you need to restrict your search to only PNG files, use glob instead:

glob('/your/folder/*.png');

The pattern *.png matches against all file names that end with “.png”, and adding the path before that ensures we only get results from inside the specified folder.

Leave a Reply

Your email address will not be published. Required fields are marked *