File Permissions and CHMOD: How to set 777 in PHP

2019-04-24 03:48发布

A question aboud file permissions when saving a file that when non existent, is created initially as new file.

Now, this all goes well, and the saved file appear to have mode 644.

What to I have to change here, in order to make the files save as mode 777?

Thanks a thousand for any hints, clues or answers. The code that I think is relevant here I have 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;
}

3条回答
霸刀☆藐视天下
2楼-- · 2019-04-24 04:02

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;
}
查看更多
看我几分像从前
3楼-- · 2019-04-24 04:20

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;
}
查看更多
【Aperson】
4楼-- · 2019-04-24 04:25

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.

查看更多
登录 后发表回答