Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default permissions for new files created with php

Tags:

php

umask

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?

like image 491
user550 Avatar asked May 21 '13 14:05

user550


People also ask

What are the default permissions of a newly created file?

New files created in your account are given a default protection of 644 (that is, 666 - 022), which grants read and write permission to the owner of the file, and read permission to the group and others.

How do I change the default permissions on a file?

To change the default permissions that are set when you create a file or directory within a session or with a script, use the umask command. The syntax is similar to that of chmod (above), but use the = operator to set the default permissions.

What is the default permission newly created file and directory?

The system default permission values are 777 ( rwxrwxrwx ) for folders and 666 ( rw-rw-rw- ) for files.


1 Answers

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
like image 98
Marc B Avatar answered Oct 09 '22 17:10

Marc B