PHP rmdir() Function: Deleting Directories
The PHP rmdir() function removes an empty directory with proper permissions, returning TRUE on success or FALSE on failure, and the article explains its parameters, return values, and provides cross‑platform code examples for safely deleting directories.
The rmdir() function in PHP attempts to delete the directory specified by $dirname ; the directory must be empty and the script must have the necessary permissions. On success it returns TRUE , otherwise FALSE and triggers an E_WARNING error.
Parameters
string $dirname – Path of the directory to be removed.
resource $context – Optional context resource for stream operations.
Return value
Returns TRUE on success, FALSE on failure.
Example 1
<?php
if (!is_dir('examples')) {
mkdir('examples');
}
rmdir('examples');
?>This example creates a directory named examples and then removes it using rmdir() .
Example 2
<?php
if (PHP_OS === 'Windows') {
exec(sprintf('rd /s /q %s', escapeshellarg($path)));
} else {
exec(sprintf('rm -rf %s', escapeshellarg($path)));
}
?>The second example demonstrates a cross‑platform approach: on Windows it runs rd /s /q , while on Unix‑like systems it uses rm -rf to delete the directory.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.