File permissions and CHMOD: How to install 777 in PHP when creating a file?

The issue of file permissions when saving a file, which when it does not exist, is created initially as a new file.

Now everything is going well, and there is 644 mode in the saved file.

What do I need to change here so that files are saved as 777 mode?

Thanks a thousand for any tips, tricks or answers. The code, which, in my opinion, matters here, I included:

 /* write to file */ self::writeFileContent($path, $value); /* Write content to file * @param string $file Save content to wich file * @param string $content String that needs to be written to the file * @return bool */ private function writeFileContent($file, $content){ $fp = fopen($file, 'w'); fwrite($fp, $content); fclose($fp); return true; } 
+7
source share
3 answers

PHP has a built-in function 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; } 
+18
source

You just need to manually set the required permissions using chmod() :

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

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 specific permissions, you need to study the umode settings. This is a process parameter that applies the standard default modification.

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

But you must think very carefully about all of these options. Files created with this mode are not completely protected from changes.

+2
source

All Articles