How to set default permissions for new files created with php

荒凉一梦 提交于 2019-12-05 15:42:11
Wes Crow

Like Linux, PHP has a chmod() command that can be invoked to change file permissions.

See the documentation here: http://php.net/manual/en/function.chmod.php

For a default setting you might try what Patrick Fisher states here: Setting the umask of the Apache user

[root ~]$ echo "umask 000" >> /etc/sysconfig/httpd
[root ~]$ service httpd restart

You can use umask() immediately before the fopen() call, but umask shouldn't be used if you're on a multi-threaded server - it'll change the mask for ALL threads (e.g. this change is at the process level), not just the one that you're about to use fopen() in.

e.g.

$old = umask(000);
fopen('foo.txt', 'w'); // creates a 0666 file
umask($old) // restore original mask

It'd be easier to simply chmod() after the fact, however:

fopen('foo.txt', 'w'); // create a mode 'who cares?' file
chmod('foo.txt', 0666); // set it to 0666
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!