PHP mkdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, mkdir() creates a new folder. You give it a path and, if you like, the permissions and an option to create missing parent folders.

It is the function you use to prepare upload folders, cache folders and log folders before saving files into them.

Syntax

Call mkdir() with the values you need. Start with this line.

PHP
$ok = mkdir('uploads', 0755);
CallWhat it does
mkdir('uploads')Creates a folder named uploads with the default permissions.
mkdir('uploads', 0755)Creates the folder with permissions that let others read but not write.
mkdir('storage/logs/2026', 0755, true)Creates the whole chain of folders in one call.

The permissions value should be written as an octal number with a leading zero, such as 0755. The final permissions are reduced by the current umask on Linux and macOS, and are mostly ignored on Windows. If the folder already exists, mkdir() returns false and shows a warning.

Library declarations
PHP
mkdir(string $directory, int $permissions = 0777, bool $recursive = false, ?resource $context = null): bool

Parameters

ParameterDescription
directoryRequired. The path of the new folder.
permissionsOptional. The permissions as an octal number. The default is 0777.
recursiveOptional. If true, missing parent folders are created too. The default is false.
contextOptional. A stream context resource. It is rarely needed.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Create a Folder

Create a folder for uploads next to the script.

PHP
<?php
$dir = __DIR__ . '/uploads';

if (mkdir($dir, 0755)) {
    echo "Folder created", PHP_EOL;
}

How It Works

  1. $dir holds the full path of the new folder.
  2. mkdir() asks the system to create it with permissions 0755.
  3. The function returns true when the folder is made.
  4. The success message is printed.

Example 2 — A Folder That Already Exists

Calling mkdir() twice on the same path fails the second time.

PHP
<?php
$dir = __DIR__ . '/reports';

mkdir($dir);
echo "First call done", PHP_EOL;

if (!@mkdir($dir)) {
    echo "Second call failed: folder already exists", PHP_EOL;
}

How It Works

  1. The first mkdir() creates the folder reports.
  2. The second mkdir() cannot create something that already exists, so it returns false.
  3. The @ hides the warning that PHP would normally show.
  4. A safer way is to test with is_dir() first.

Example 3 — Create Nested Folders

Pass true as the third argument to create every missing folder in the path.

PHP
<?php
$dir = __DIR__ . '/storage/logs/2026';

if (!is_dir($dir)) {
    mkdir($dir, 0755, true);
}

echo is_dir($dir) ? "Ready: storage/logs/2026" : "Missing", PHP_EOL;

How It Works

  1. is_dir() checks whether the folder is already there.
  2. mkdir() with true as the third argument creates storage, logs and 2026 together.
  3. Without that third argument the call would fail because the parents do not exist.
  4. The final is_dir() confirms that the folder was created.

Frequently Asked Questions

It creates a new folder at the path you give. It returns true on success and false on failure.
Pass true as the third argument, for example mkdir('a/b/c', 0755, true). PHP will create every missing parent.
The folder is already there. Use is_dir() before calling mkdir() to skip the call when it exists.
0755 is a good default. It lets the owner write and everyone else read. Avoid 0777 on a live server.
The user running PHP cannot write in the parent folder. Fix the folder permissions or ownership on the server.

Next: move_uploaded_file()

move_uploaded_file() moves a file that was just uploaded through an HTML form from its tem.

move_uploaded_file() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful