Menu

How to Create Folder In PHP If Not Exists

PHP By May 25, 2014 No Comments

How to create folder in PHP if its not exists


Hello friends today i am going to write this article which is very easy and useful for every developer. Many we need this and use this. Create folder in php using the php is very easy task…. how ?

There is an inbuilt function mkdir() which creates the folder at your given path. mkdir() take optional 3 parameters total 4 parameters.

mkdir() parameters

  1. Pathname
  2. Mode
  3. Recursive
  4. Context

In the above parameters we need 3 parameters most – Pathname, Mode, Recursive for Context refer this. So first we will create the simple directory in our first step than we will create directory inside directory lets start now.

Step 1. Create single folder

<?php 
$path="gallery";
mkdir($path,0777);
?>

In the above code if you output it will create the directory named “Gallery”. Suppose you have a directory and you want your new directory to create under that than you have to just change the path and it will create it. Suppose you have User now you need employee under user (User – > Employee) than we will do it just like the below.


Step 2. Create Multiple directory

<?php 
$path="user/employee";
mkdir($path,0777,true);
?>

In the above code we have used the 3 parameter and third one is recursive. Which create the User directory than it will create the Employee under the User.

Before creating any directory be sure that it is not already exists if it is there than it will give you the error. So always check first than create the folder.

<?php 
    $path="employee";
    if(!is_dir($path))
    {
        $check=mkdir($path,0777);
    }
    else
    {
        echo "Already";
    }
?>

Hop you like the Create Folder in php tutorial. If you want to give us any advice or comment please comment below. So that we will serve you better.

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *