Parsing Directories In PHP. What You Need To Know

Posted by TotalDC

We already covered PHP File System and Include and Require in PHP. Now you will learn what parsing directories in PHP is, how to process directories or folders using PHP.

Working With Directories In PHP

Similarly to work with files, PHP also allows you to work with directories on the file system. For example – open directory and read data in it, create or delete directory, list all files in the directory etc.

Creating A New Directory

You can create a new and empty directory by calling the PHP mkdir() function with the path and name of the directory to be created. Here is an example for you:

// The directory path
$dir = "testdir";
// Check if file exist
    // Create directory
        print "Directory created successfully.";
    } else{
        print "ERROR: Directory could not be created.";
} else{
    print "ERROR: Directory already exists.";

For mkdir() to work the parent directories in the directory path parameter has to already exist. For example if you specify the directory path as testdir/subdir than the tesdir has to to be created already. If it does not, than you will get an error.

How To Copy Files From One Location To Another

You are able to copy file from one location to another by calling PHP copy() function and providing file’s source and wanted destination as arguments. Just know that if destination file already exists it will be overwritten.


// Primary file location
$file = "example.txt";
// Where you want to copy your file
$newfile = "backup/example.txt";
// Check if file exist
    // Copy the file
    if(copy($file, $newfile)){
        print "File copied successfully.";
    } else{
        print "ERROR: File could not be copied.";
} else{
    print "ERROR: File does not exist.";

Just remember that both start and destination directories must exist. If not – you will get an error.

How To List All Files In A Directory

To list files and directories in a specified location you can use scandir() function. For example let’s create a PHP function that will list all files in a directory recursively.

// Define a functio
function outputFiles($path){
    // Check if directory exist
    if(file_exists($path) && is_dir($path)){
        // Scan the files in this directory
        $result = scandir($path);
        // Filter out the directories
        $files = array_diff($result, array('.', '..'));
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                    // Display filename
                    print $file . "<br>";
                } else if(is_dir("$path/$file")){
                    // Recursively call the function if directories found
        } else{
            print "ERROR: No files found in the directory.";
    } else {
        print "ERROR: The directory does not exist.";
// Call the function

How To List All Files By Type

When you will be working with files you will find yourself in a situation where you need to find files of a specific type, for example all .png files in a directory. You can do this with glob() function which matches files based on the pattern. In this example all files with .text extension will be listed.

// Loop through the directory

foreach(glob("documents/*.txt") as $file){
    print basename($file) . " (size: " . filesize($file) . " bytes)" . "<br>";

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: