PHP | mkdir() Function

PHP mkdir() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mkdir() Function.

The built-in PHP mkdir() function creates a directory.

Syntax

Following below is the syntax to use this function -

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


This built-in function can attempt to create a directory specified by pathname.

Return Value

This built-in PHP function returns true on success and false on failure.

Example1

Try out the below example -

<?php
   mkdir("/PhpProject/testing");
   echo "Directory created successfully!!!";
?>

Output

When the above code is executed, it will produce the following result -

Directory created successfully!!!

Example2

Try out the below example -

<?php
   $structure = "/PhpProject/test1/test2/test3";

   if(!mkdir($structure, 0777, true)) {
      echo "Failed to create folders...";
   } else {	
       echo "Nested structure created successfully!!!";
   }
?>

Output

When the above code is executed, it will produce the following result -

Nested structure created successfully!!!


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the move_uploaded_file() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.