File Permissions and CHMOD: How to set 777 in PHP upon file creation? File Permissions and CHMOD: How to set 777 in PHP upon file creation? php php

File Permissions and CHMOD: How to set 777 in PHP upon file creation?


PHP has a built in function called bool chmod(string $filename, int $mode )

http://php.net/function.chmod

private function writeFileContent($file, $content){    $fp = fopen($file, 'w');    fwrite($fp, $content);    fclose($fp);    chmod($file, 0777);  //changed to add the zero    return true;}


You just need to manually set the desired permissions with chmod():

private function writeFileContent($file, $content){    $fp = fopen($file, 'w');    fwrite($fp, $content);    fclose($fp);    // Set perms with chmod()    chmod($file, 0777);    return true;}


If you want to change the permissions of an existing file, use chmod (change mode):

$itWorked = chmod ("/yourdir/yourfile", 0777);

If you want all new files to have certain permissions, you need to look into setting your umode. This is a process setting that applies a default modification to standard modes.

It is a subtractive one. By that, I mean a umode of 022 will give you a default permission of 755 (777 - 022 = 755).

But you should think very carefully about both these options. Files created with that mode will be totally unprotected from changes.