PHP create nested directories
Asked Answered
J

10

30

I need help with a function to create a 2 level directory for the following situations:

  1. The desired sub-directory exists in the parent directory, do nothing.
  2. Parent directory exists, sub-directory does not exist. Create only the sub-directory.
  3. Neither parent directory, nor the sub-directory exists, First create parent directory, then sub-directory.
  4. If Any of the directory was not created successfully, return FALSE.

Thanks for the help.

Johan answered 5/7, 2011 at 8:39 Comment(0)
F
73

Use the third parameter of mkdir():

recursive Allows the creation of nested directories specified in the pathname. Defaults to FALSE.

$path = '/path/to/folder/with/subdirectory';
mkdir($path, 0777, true);
Flexion answered 5/7, 2011 at 8:42 Comment(2)
@Paulocoghi You are right. This behaviour is different from Linux' mv, which simply ignores existing pathsFlexion
use if(!is_dir($path)) { mkdir($path, 0777, true); }Bacchant
A
6

recursive Allows the creation of nested directories specified in the pathname. but did not work for me!! for that here is what i came up with!! and it work very perfect!!

$upPath = "../uploads/RS/2014/BOI/002";   // full path 
$tags = explode('/' ,$upPath);            // explode the full path
$mkDir = "";

    foreach($tags as $folder) {          
        $mkDir = $mkDir . $folder ."/";   // make one directory join one other for the nest directory to make
        echo '"'.$mkDir.'"<br/>';         // this will show the directory created each time
        if(!is_dir($mkDir)) {             // check if directory exist or not
          mkdir($mkDir, 0777);            // if not exist then make the directory
        }
    }
Actinology answered 15/4, 2014 at 4:40 Comment(0)
A
3

you can try using file_exists to check if a folder exists or not and is_dir to check if it is a folder or not.

 if(file_exists($dir) && is_dir($dir))

And to create a directory you can use the mkdir function

Then the rest of your question is just manipulating this to suit the requirements

Alcheringa answered 5/7, 2011 at 8:42 Comment(0)
B
1

See mkdir, in particular the $recursive parameter.

Be answered 5/7, 2011 at 8:43 Comment(0)
G
1

How much i suffered.. and got this script..

function recursive_mkdir($dest, $permissions=0755, $create=true){
    if(!is_dir(dirname($dest))){ recursive_mkdir(dirname($dest), $permissions, $create); }  
    elseif(!is_dir($dest)){ mkdir($dest, $permissions, $create); }
    else{return true;}
}
Gazpacho answered 16/7, 2016 at 13:8 Comment(2)
Is this just not formatted or do you seriously write code like this?Nalchik
@buffalo, if had seriously read the information about answer (either my comment got this script or even post date) it says enough. moreover, you could just edit post, instead of irony, you had enough rep ;) cheers.Gazpacho
O
1

Starting with PHP 8 (2020-11-24), you can use named parameters:

<?php
mkdir('March/April', recursive: true);

https://php.net/function.mkdir

Oppugn answered 25/11, 2020 at 20:1 Comment(0)
L
0

The function you're looking for is MKDIR. Use the last parameter to recursively create directories. And read the documentation.

Larry answered 5/7, 2011 at 8:43 Comment(0)
E
0

As of PHP 5.0+ mkdir has a recursive parameter which will create any missing parents.

Eldred answered 5/7, 2011 at 8:44 Comment(0)
T
0
// Desired folder structure
$structure = './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0744, true)) {
    die('Failed to create folders...');
}

Returns TRUE on success or FALSE on failure.

PHP: mkdir - Manual

Thine answered 5/7, 2011 at 8:47 Comment(0)
P
0

You can use is_dir and mkdir

if (!is_dir($path)) 
{
    @mkdir($path, 0777, true);
}
Podophyllin answered 7/7, 2022 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.