Get All the Files from a Directory Using PHP
by c.bavota | Posted in Tutorials | 7 comments
I was developing a WordPress site for a client who required multiple images for each of the products that they offered. Instead of uploading each one through the WP uploader, I decided the best approach would be to first upload them all into a specific folder using FTP, and then use the opendir function in PHP to retrieve them all.
For this example all of my images were added to a folder called products in the wp-content/uploads/ directory.
First we need to figure out where the uploads folder is located. Luckily, WordPress has a function made just for that purpose.
$uploads = wp_upload_dir();
Now let’s start with the code to get all the files in the products directory. We’re going to store them in an array.
if ($dir = opendir($uploads['basedir'].'/products')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
Now our $images array contains all the images we added to the products directory.
To display the images, all we have to do is something like this:
echo '<ul>';
foreach($images as $image) {
echo '<li><img src="';
echo $uploads['baseurl'].'/products/'.$image;
echo '" alt="" /></li>';
}
echo '</ul>';
The above code will have all the images displayed in an unordered list.
Here is everything put together:
<?php
$uploads = wp_upload_dir();
if ($dir = opendir($uploads['basedir'].'/products')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
echo '<ul>';
foreach($images as $image) {
echo '<li><img src="';
echo $uploads['baseurl'].'/products/'.$image;
echo '" alt="" /></li>';
}
echo '</ul>';
?>
Here is an alternative which removes having the need for an array:
<?php
$uploads = wp_upload_dir();
echo '<ul>';
if ($dir = opendir($uploads['basedir'].'/products')) {
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
echo '<li><img src="';
echo $uploads['baseurl'].'/products/'.$file;
echo '" alt="" /></li>';
}
}
closedir($dir);
}
echo '</ul>';
?>
If you’re not using WordPress, all you have to do is add the absolute file path to the opendir function so that it looks something like this:
opendir('/home/maindir/public_html/products')



ok not for my wordpress site but for some data I need to provide, this gives me quite a good start. Thanks for that
Good stuff. I like the use of the array. Having a problem displaying the first image in an upload folder. Any suggestions to do this?
Thanks!
For me, this code only prints out the tags for the unordered list. The “foreach” loop doesn’t output anything. Ideas?
PS – The second version of the code you give needs not only the base URL but also, the name of the folder the images were uploaded into. Once I made that change, *that* version of the code worked.
So… echo $uploads['baseurl'].’/yourfolder/’.$file;
Thanks, I really needed this tut.
Thanks for pointing that out. I fixed it in the above code.
Thanks for tutorials. this post powerful.
Great, thanks for the code, just what I needed.