Problem with PHP mkdir!

I am trying to create several such files:

@mkdir("photos/$cat/$sku", 0777, true)

it creates the first directory with permissions 0777, but when it creates the second, it uses 000 as perms, so it fails to create the third.

Workaround is this please?

Thanks Richard.

+3
source share
4 answers

This solved the problem:

$a = @mkdir("photos/$cat/", 0777);
    @chmod("photos/$cat/", 0777);
    $b = @mkdir("photos/$cat/$sku/", 0777);
    @chmod("photos/$cat/$sku/", 0777);

but why not use recursion on mkdir?

+1
source

I did this and it works great:

    if (!is_dir($path)) {
        $dirs = explode('/', $path);
        $i = 0;
        $subdir = '';
        foreach ($dirs as $dir) {
            if($i > 0){$dir = '/' . $dir;}
            $subdir .= $dir;
            if(!is_dir(DIR_CACHE . $subdir)){@mkdir(DIR_CACHE . $subdir);@chmod(DIR_CACHE . $subdir, 0777);}

            $i++;
        }
    }

So all you have to do is define your path ($ path = photos / $ cat / $ sku)

+1
source

, , , , , mkdir,

0

chmod ?

mkdir("photos/$cat", 0777, true);
chmod("photos", 0777);
chmod("photos/$cat", 0777);
mkdir("photos/$cat/$sku", 0777);
chmod("photos/$cat/$sku", 0777);
0

Source: https://habr.com/ru/post/1746122/


All Articles