Hi I am running a centos server and I want to know how I can set the default chmod of a newly created file that is created by php like fopen. At the moment it is doing 644 but I want 666 so where can I specify this setting?
Asked
Active
Viewed 2.2k times
2 Answers
5
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

Marc B
- 356,200
- 43
- 426
- 500
-
I only need `umask($old)` when I want to restore the original mask for the current script, right? Or does the `umask` call make a permanent change? – Adam Dec 06 '16 at 20:11
2
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
-
Yes I know this but i am talking about the default value without changing it :) – user550 May 21 '13 at 14:27
-
I edited to better address your question. This is supposed to work with Centos. – Wes Crow May 21 '13 at 14:32