← Back to PHP Course | Chapter 9: File Handling | Lesson 6 of 8

PHP Directory Functions

Directory functions let PHP look inside folders, like flipping through a filing cabinet. It can list what is inside, make new folders, or remove them.
Syntax
php
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
<?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);
?>

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
<?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"));
?>

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
<?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";
?>

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
<?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);
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using rmdir on a directory that is not empty, which fails.
  2. Forgetting that scandir includes . and .. in the result.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.