3

When I call mkdir('/tmp/d/e/e/p/p/a/t/h/', 0777, true); I needed all the subdirectories created so far to have the specified chmod: 0777.

What can be the shortest way for this? I noticed that it did not happen.

root@server [/tmp]# ls -la /tmp/d/e/e/p/p/a/t/h/
total 8
drwxr-xr-x 2 user group 4096 Aug  6 12:59 ./
drwxr-xr-x 3 user group 4096 Aug  6 12:59 ../

Code:

<?php
mkdir('/tmp/d/e/e/p/p/a/t/h', 0777, true);
?>

Please have a look.

chresse
  • 5,486
  • 3
  • 30
  • 47
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41

2 Answers2

3

It is like jack sleight said on a php.net (http://php.net/manual/en/function.mkdir.php#96990)

you have to run a chmod for each of the directories of /tmp/d/e/e/p/p/a/t/h.

You can do it ie with a foreach loop. Something like this should work:

$path = "/tmp/d/e/e/p/p/a/t/h";
$dirs = explode("/", $path);
$cDirs = "";
foreach($dirs as $cDir) {
    $cDirs .= "/".$cDir;
    chmod($cDir, 0777);
}

HINT: If you are under linux you simply can run:

$path = "/tmp/d/e/e/p/p/a/t/h";
exec("mkdir -R ".$path);
exec("chmod -R 777 ".$path);
chresse
  • 5,486
  • 3
  • 30
  • 47
1

I'd do it in a single line:

exec('install -d -m 0777 /tmp/d/e/e/p/p/a/t/h');

This creates all directories recursively with the specified mask in one step.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151