What You Need To Know About Parsing Directories In PHP
Posted by TotalDCWe already covered PHP File System and Include and Require in PHP. Now you will learn what parsing directories in PHP is, and how to process directories or folders using PHP.
Working With Directories With PHP
Similarly to working with files, PHP also allows you to work with directories on the file system. For example – open a directory and read data in it, create or delete a directory, list all files in the directory, etc.
Creating A New Directory With PHP
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:
<?php
// The directory path
$dir = "testdir";
// Check if file exist
if(!file_exists($dir)){
// Create directory
if(mkdir($dir)){
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 have to already exist. For example, if you specify the directory path as testdir/subdir then the tesdir has to be created already. If it does not, then you will get an error.
How To Copy Files From One Location To Another With PHP
You can copy a file from one location to another by calling the PHP copy() function and providing the file’s source and wanted destination as arguments. Just know that if the destination file already exists it will be overwritten.
<?php
// Primary file location
$file = "example.txt";
// Where you want to copy your file
$newfile = "backup/example.txt";
// Check if file exist
if(file_exists($file)){
// 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 With PHP
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.
<?php
// 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){
if(is_file("$path/$file")){
// Display filename
print $file . "<br>";
} else if(is_dir("$path/$file")){
// Recursively call the function if directories found
outputFiles("$path/$file");
}
}
} else{
print "ERROR: No files found in the directory.";
}
} else {
print "ERROR: The directory does not exist.";
}
}
// Call the function
outputFiles("mydir");
?>
How To List All Files By Type With PHP
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.
<?php
// Loop through the directory
foreach(glob("documents/*.txt") as $file){
print basename($file) . " (size: " . filesize($file) . " bytes)" . "<br>";
}
?>