Mar
30
2010

Get All the Files from a Directory Using PHP

by   |  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')

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , , , ,

Short URL: http://bit.ly/cpOfM7

Discussion 7 Comments

  1. Guy on May 21, 2010 at 9:13 am

    ok not for my wordpress site but for some data I need to provide, this gives me quite a good start. Thanks for that

  2. UnkFrank on September 23, 2010 at 5:19 am

    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!

  3. Sara on November 14, 2010 at 3:17 am

    For me, this code only prints out the tags for the unordered list. The “foreach” loop doesn’t output anything. Ideas?

  4. Sara on November 14, 2010 at 3:23 am

    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. :)

    • c.bavota on November 14, 2010 at 2:32 pm

      Thanks for pointing that out. I fixed it in the above code.

  5. Joannie Markie on January 14, 2011 at 6:30 pm

    Thanks for tutorials. this post powerful.

  6. Karl on February 17, 2011 at 12:37 pm

    Great, thanks for the code, just what I needed.