PHP Directory Functions
In this page:
mkdir($path);
rmdir($path);
$items = scandir($path);
is_dir($path);
file_exists($path);
Creating Directories
scandir($path) returns an array of every file and subdirectory name inside a given directory, including the special '.' and '..' entries that represent the current and parent directory.
Example: Creating Directories
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Declare `$items`, set to `scandir("sample_dir")`
$items = scandir("sample_dir");
// Print a human-readable dump of `$items`
print_r($items);
?>
Login to try C/C++/Java/PHP code in the editor
Removing Directories
is_dir() and file_exists() let you check whether a given path is a directory or exists at all before you try to read from or write to it, avoiding warnings from operating on a path that isn't there.
Example: Removing Directories
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Print a detailed dump (with types) of `is_dir("sample_dir")`
var_dump(is_dir("sample_dir"));
// Print a detailed dump (with types) of `file_exists("sample_dir")`
var_dump(file_exists("sample_dir"));
?>
Login to try C/C++/Java/PHP code in the editor
Listing Directory Contents
mkdir($path) creates a new directory, and passing true as the third argument (recursive) lets it create any missing parent directories along the way in a single call.
Example: Listing Directory Contents
<?php
// Call `mkdir("parent/child", 0755, true)`
mkdir("parent/child", 0755, true);
// Print `is_dir("parent/child") ? "Created including parent" : "Failed"` to the output
echo is_dir("parent/child") ? "Created including parent" : "Failed";
?>
Login to try C/C++/Java/PHP code in the editor
Working Directories
glob($pattern) finds all paths matching a wildcard pattern like '*.php', which is often more convenient than scandir() plus manual filtering when you only care about files of a specific type.
Example: Working Directories
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Call `file_put_contents("sample_dir/a.php", "")`
file_put_contents("sample_dir/a.php", "");
// Call `file_put_contents("sample_dir/b.txt", "")`
file_put_contents("sample_dir/b.txt", "");
// Declare `$phpFiles`, set to `glob("sample_dir/*.php")`
$phpFiles = glob("sample_dir/*.php");
// Print a human-readable dump of `$phpFiles`
print_r($phpFiles);
?>
Login to try C/C++/Java/PHP code in the editor
Checking Directories
rmdir() removes an empty directory, but will fail on a directory that still contains files — deleting a non-empty directory tree requires recursively deleting its contents first.
Example: Checking Directories
<?php
// Call `mkdir("empty_dir")`
mkdir("empty_dir");
// Call `rmdir("empty_dir")`
rmdir("empty_dir");
// Print `file_exists("empty_dir") ? "Still exists" : "Removed successfully"` to the output
echo file_exists("empty_dir") ? "Still exists" : "Removed successfully";
?>
Login to try C/C++/Java/PHP code in the editor
- Using
rmdiron a directory that is not empty, which fails. - Forgetting that
scandirincludes.and..in the result. - Not checking that a directory exists before creating it with
mkdir.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: