How to Get All File Names for a Directory with PHP

I fought with this for a while. I had to use PHP to return all file names in a directory with their respective size and types. I also had to return only files and not directories, but this guide will show you how to do both just in case you might need directories too.

Here is what the following code will return for a directory with three files in it:

files

Now here’s the code that finally worked for me when I put the code in a file named get_file_names.php in the html directory which also held the “uploads” directory which it reads from :

//get the lastest file uploaded in uploads/
$path = “uploads”;    
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = “{$path}/{$entry}”;
//Check whether the entry is a file etc.:
    if(is_file($filepath)) {
    $latest_filename = $entry;
    $file_type = filetype($filepath);//get file type.
    $file_size = filesize($filepath);//get file size.
    echo “$latest_filename<br />Type: $file_type<br />Size: $file_size<hr />”;
    }//end if is file etc.
}//end while going over files in uploads dir.

Wrap that in PHP tags and you got your get_file_names.php script.

How to Get Both Files and Directories:

To get both files and directories, the script is even simpler, simply take out the last if statement so your final code looks like:

//get ELEMENTS in uploads/
$path = “uploads”;    
$d = dir($path);
    while (false !== ($entry = $d->read())) {
    $filepath = “{$path}/{$entry}”;
    $latest_ctime = filectime($filepath);//like: 1402783996 that is timestamp so highest is latest.
    $latest_filename = $entry;
    $file_type = filetype($filepath);//get file type.
    $file_size = filesize($filepath);//get file size.
    echo “$latest_filename<br />Type: $file_type<br />Size: $file_size<hr />”;
    }//end while going over files AND DIRECTORIES in uploads dir.

Running the code above will get the following results:

files-with-dirs

How to show Files and only Directories with Names and not dot or dotdot:

Notice the “.” and the “..” those are directories in which you may not wish to show that represent the current directory and the parent directory respectively. To get redi of the dot and the dotdot, simply check for them like in the following code example:

<?php
//get the lastest file uploaded in uploads/
$path = “uploads”;    
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = “{$path}/{$entry}”;
//Check whether the entry is a file etc.:
$latest_filename = $entry;
    if($latest_filename != “.” && $latest_filename != “..”) {
    $latest_filename = $entry;
    $file_type = filetype($filepath);//get file type.
    $file_size = filesize($filepath);//get file size.
    echo “$latest_filename<br />Type: $file_type<br />Size: $file_size<hr />”;
    }//end if is file etc.
}//end while going over files in excel_uploads dir.

?>

The above code returns:

without_dots

 

How to Show Only Certain File Types:

In the next code snippet, I show you how to only show .html files from a specific directory. Here is that PHP code:

//path to directory to scan
$directory = “uploads/”;
 
//get all files files with a .html extension.
$files = glob($directory . “*.html”);
 
//print each file name
foreach($files as $filename)
{
echo $filename;
echo “<br>”;
}

Leave a Reply

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