Example
| The code is as follows: |
Copy code |
<? Php /** * Makes directory and returns BOOL (TRUE) if exists OR made. * * @ Param $ path Path name * @ Return bool */ Function rmkdir ($ path, $ mode = 0755 ){ $ Path = rtrim (preg_replace (array ("/\/", "// {2,}/"), "/", $ path ),"/"); $ E = explode ("/", ltrim ($ path ,"/")); If (substr ($ path, 0, 1) = "/"){ $ E [0] = "/". $ e [0]; } $ C = count ($ e ); $ Cp = $ e [0]; For ($ I = 1; $ I <$ c; $ I ++ ){ If (! Is_dir ($ cp )&&! @ Mkdir ($ cp, $ mode )){ Return false; } $ Cp. = "/". $ e [$ I]; } Return @ mkdir ($ path, $ mode ); } ?> |
Example 2
Somehow the recursive version of mkdir didn't work for me on Mac and the workaraounds listed
Below alsow didn't work for me, so heres my solution:
| The code is as follows: |
Copy code |
<? Php Function mkdir_r ($ dirName, $ rights = 0777 ){ $ Dirs = explode ('/', $ dirName ); $ Dir = ''; Foreach ($ dirs as $ part ){ $ Dir. = $ part .'/'; If (! Is_dir ($ dir) & strlen ($ dir)> 0) Mkdir ($ dir, $ rights ); } } ?> Tested and works ;) |
Example 3
| The code is as follows: |
Copy code |
Function mkdirs ($ dir) { If (! Is_dir ($ dir )) { If (! Mkdirs (dirname ($ dir ))){ Return false; } If (! Mkdir ($ dir, 0777 )){ Return false; } } Return true; } Function rmdirs ($ dir) { $ D = dir ($ dir ); While (false! ==( $ Child = $ d-> read ())){ If ($ child! = '.' & $ Child! = '..'){ If (is_dir ($ dir. '/'. $ child )) Rmdirs ($ dir. '/'. $ child ); Else unlink ($ dir. '/'. $ child ); } } $ D-> close (); Rmdir ($ dir ); } |
The three functions that create directories all have their own advantages. I didn't test them one by one here, but I only used the second one and I felt very good.