In the previous chapter, you've learned how to handle files in PHP. Similarly, PHP also provides functionality to manage directories on the file system. For instance, you can open a directory to read its contents, create or delete directories, list all files within a directory, and more.
You can create a new, empty directory using the PHP mkdir()
function. Specify the path and name of the directory you want to create, as demonstrated in the example below:
<?php
// The directory path
$dir = "testdir";
// Check the existence of directory
if(!file_exists($dir)){
// Attempt to create directory
if(mkdir($dir)){
echo "Directory created successfully.";
} else{
echo "ERROR: Directory could not be created.";
}
} else{
echo "ERROR: Directory already exists.";
}
?>
To use the mkdir()
function successfully, the parent directories specified in the directory path parameter must already exist. For instance, if you specify the directory path as testdir/subdir
, the testdir
directory must exist beforehand, or PHP will generate an error.
You can duplicate a file from one location to another using the PHP copy()
function. Provide the file's source and destination paths as arguments. If the destination file already exists, it will be overwritten. Here's an example that copies the file "example.txt" into a backup folder:
<?php
// Source file path
$file = "example.txt";
// Destination file path
$newfile = "backup/example.txt";
// Check the existence of file
if(file_exists($file)){
// Attempt to copy file
if(copy($file, $newfile)){
echo "File copied successfully.";
} else{
echo "ERROR: File could not be copied.";
}
} else{
echo "ERROR: File does not exist.";
}
?>
To make this example function correctly, ensure both the target directory, backup, and the source file, "example.txt", already exist. Otherwise, PHP will encounter an error.
You can utilize the PHP scandir()
function to retrieve a list of files and directories within a specified path.
Next, we'll create a custom function to recursively list all files in a directory using PHP. This script is particularly useful for navigating deeply nested directory structures.
<?php
// Define a function to output files in a directory
function outputFiles($path){
// Check directory exists or not
if(file_exists($path) && is_dir($path)){
// Scan the files in this directory
$result = scandir($path);
// Filter out the current (.) and parent (..) 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
echo $file . "<br>";
} else if(is_dir("$path/$file")){
// Recursively call the function if directories found
outputFiles("$path/$file");
}
}
} else{
echo "ERROR: No files found in the directory.";
}
} else {
echo "ERROR: The directory does not exist.";
}
}
// Call the function
outputFiles("mydir");
?>
When managing directory and file structures, there are times when you may need to identify specific types of files within a directory, such as listing only .text
or .png
files. This task can be easily accomplished using the PHP glob()
function, which searches for files based on a specified pattern.
In the PHP example below, the code will search within the documents directory and display a list of all files with the .text
extension. Note that this search does not include subdirectories.
<?php
/* Search the directory and loop through
returned array containing the matched files */
foreach(glob("documents/*.txt") as $file){
echo basename($file) . " (size: " . filesize($file) . " bytes)" . "<br>";
}
?>
The glob()
function is versatile and can also be used to retrieve all files within a directory, including its subdirectories. The function demonstrated in the following example will recursively list all files within a directory, similar to how we achieved it earlier with the scandir()
function.
<?php
// Define a function to output files in a directory
function outputFiles($path){
// Check directory exists or not
if(file_exists($path) && is_dir($path)){
// Search the files in this directory
$files = glob($path ."/*");
if(count($files) > 0){
// Loop through retuned array
foreach($files as $file){
if(is_file("$file")){
// Display only filename
echo basename($file) . "<br>";
} else if(is_dir("$file")){
// Recursively call the function if directories found
outputFiles("$file");
}
}
} else{
echo "ERROR: No such file found in the directory.";
}
} else {
echo "ERROR: The directory does not exist.";
}
}
// Call the function
outputFiles("mydir");
?>