Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. mkdir() / rmdir() / unlink() / copy() / rename()

mkdir() / rmdir() / unlink() / copy() / rename() Since: PHP 4(2000)

Creates, deletes, copies, moves, and changes permissions of files and directories. These functions cover the fundamental operations for working with the file system.

Syntax

// Creates a directory.
mkdir($directory, $permissions, $recursive);

// Deletes an empty directory.
rmdir($directory);

// Deletes a file.
unlink($filename);

// Copies a file.
copy($source, $dest);

// Renames or moves a file or directory.
rename($from, $to);

// Changes the permissions of a file.
chmod($filename, $permissions);

Function List

FunctionDescription
mkdir($directory, $permissions, $recursive)Creates a directory. Pass true as the third argument to create intermediate directories recursively.
rmdir($directory)Deletes an empty directory. Fails if the directory contains any files.
unlink($filename)Deletes a file. Cannot be used to delete directories.
copy($source, $dest)Copies a file. If a file with the same name already exists at the destination, it is overwritten.
rename($from, $to)Renames a file or directory. Specifying a path in a different directory moves the file as well.
chmod($filename, $permissions)Changes the permissions of a file. Permissions must be specified as an octal number.

Return Value

All functions return true on success and false on failure. Common causes of failure include insufficient permissions, an invalid path, or a directory that is not empty.

Sample Code

<?php
// Creates a directory.
mkdir("/var/www/html/uploads", 0755);
echo "Directory created.";

// Creates directories recursively. Intermediate directories are created all at once even if they do not exist.
mkdir("/var/www/html/data/2025/04", 0755, true);
echo "Nested directories created.";

// Checks whether the directory exists before creating it.
$dir = "/var/www/html/cache";
if (!is_dir($dir)) {
    mkdir($dir, 0755, true);
}

// Deletes a file.
if (file_exists("/var/www/html/temp.txt")) {
    unlink("/var/www/html/temp.txt");
    echo "File deleted.";
}

// Deletes an empty directory.
rmdir("/var/www/html/old_cache");
echo "Directory deleted.";

// Copies a file.
copy("/var/www/html/config.json", "/var/www/html/config_backup.json");
echo "Backup created.";

// Moves a file.
rename("/var/www/html/temp/report.pdf", "/var/www/html/archive/report.pdf");
echo "File moved.";

// Renames a file.
rename("/var/www/html/old_name.txt", "/var/www/html/new_name.txt");
echo "File renamed.";

// Changes permissions.
chmod("/var/www/html/pvt/config.php", 0640); // Owner: read/write. Group: read-only.
echo "Permissions changed.";

// Practical example: delete all files inside a directory.
$dir = "/var/www/html/temp";
$files = glob($dir . "/*");
foreach ($files as $file) {
    if (is_file($file)) {
        unlink($file);
    }
}
rmdir($dir); // After removing all contents, delete the now-empty directory.
echo "Directory and all its contents deleted.";

Notes

These are the core functions for file system operations. Passing true as the third argument to mkdir() creates all intermediate directories in one call. Because rmdir() can only delete an empty directory, you must first remove all files inside it with unlink().

rename() can both rename and move a file — just specify a path in a different directory as the destination. However, moving across different file systems may fail. In that case, use copy() to duplicate the file and then unlink() to delete the original.

Always specify permissions for chmod() as an octal number — either with a leading zero (e.g., 0755) or by converting with octdec("755"). Passing the decimal 755 will set unintended permissions. To check whether a file or directory exists, use file_exists() / is_file() / is_dir().

If you find any errors or copyright issues, please .