Jump to content

list all files in a directory


bill-lancaster

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/286700-list-all-files-in-a-directory/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.