简体   繁体   中英

PHP - mkdir : Create a folder with the same rights as it's parent

Is there a way in PHP to create a folder with the same permission as it's parent .

For example: I have a folder /a/b with 0755 permissions, I want to create the folder a/b/c with the same permissions. 0755 in this case.

As far as i know, PHP creates folders with 0777 permission by default with the mkdir function. Is there a smart way to detect the parent folder permission an then create that child folder with the same chmod?

Edit: Here I am looking for a solution to create a floder with mkdir not how to retrieve a file/folder permissions.

@ArtisticPhoenix solution is what i looked for.

Thank you.

You can try this thing I just wiped up (untested)

chmod($filename, substr(sprintf('%o', fileperms(dirname($filename))),-4));

http://php.net/manual/en/function.chmod.php

http://php.net/manual/en/function.fileperms.php

I think the other functions are pretty self explanatory, or basic enough they don't warrant a link. dirname($filename) of course being the files directory.

This substr(sprintf('%o', fileperms(...)),-4) stuff comes from the documentation page:

Example #1 Display permissions as an octal value

  echo substr(sprintf('%o', fileperms('/tmp')), -4); echo substr(sprintf('%o', fileperms('/etc/passwd')), -4); 

Which seems like something we would want to use, because that is what chmod wants, but it may be worth testing if that is required or not (maybe it's smart enough to understand it). I haven't really used fileperms before. In most cases is_writable is all I've needed.

Hope it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM