bill-lancaster Posted March 4, 2014 Share Posted March 4, 2014 I'm new to this forum and also new to php coding. I wish to list all the files in a given directory on my website and have seen quite a few examples but cant get any to work for me. My code is located in /Gallery and I wish to list all files in /Gallery/Thumbnails. What is the best way to go about this? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 4, 2014 Share Posted March 4, 2014 (edited) Easiest way is using glob Example foreach(glob('Gallery/thumbnails/*') as $file) { echo $file; } If you want to filter the images, by file extension use this instead glob('Gallery/thumbnails/*.{gif,jpg,png,bmp}', GLOB_BRACE) Edited March 4, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 4, 2014 Share Posted March 4, 2014 Try this simple example, <?php $dir = dirname(__FILE__)."/"; //Get the directory where this script is located echo "This is the full path: ".$dir echo "<br />\n"; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { //Checks its a directory if ($dh = opendir($dir)) { //Access the directory while (($file = readdir($dh)) !== false) { //read files/folder from the directory echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; //display the found item directory } closedir($dh); } } ?> it should give you the full of where the script is and what files and folders are inside. The most common problem is getting the path right. You could change dir in the above script to $dir = dirname(__FILE__)."/Thumbnails/"; //Get the directory where this script is located If the script is in the Gallery directory.. Hope this helps if not then just lets us know what happened Quote Link to comment Share on other sites More sharing options...
Solution bill-lancaster Posted March 5, 2014 Author Solution Share Posted March 5, 2014 Thanks for the informed replies. I tried the first reply and found that I needed the full path to 'Gallery' and discovered getcwd() Then realised the second suggestion anticipated this issue! Thanks again - problem solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.