English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP mkdir() Function Usage and Example

PHP Filesystem Reference Manual

The mkdir() function can create a directory, and this function returns true when successful and false when failed.

Syntax

bool mkdir ( string $pathname[, int $mode = 0777 [, bool $recursive = FALSE[, resource $context]]])

This function attempts to create the directory specified by pathname.

Example1

Using mkdir to create a directory

<?php
   mkdir("/PhpProject/testing");
   echo "Directory creation successful!!!";
?>

Output result

Directory creation successful!!!

Example2

Create the root directory and subdirectories at the same time, multiple directories

<?php
   $structure = "/PhpProject/test1/test2/test3";
   if(!mkdir($structure, 0777, true)) {
      echo "Unable to create folder...";
   } else {	
       echo "All directories created successfully!!!";
   }
?>

Output result

All directories created successfully!!!

PHP Filesystem Reference Manual