文件权限和CHMOD:如何在创建文件时在PHP中设置777?

Sam*_*Sam 11 php file-permissions file chmod

保存不存在的文件时的问题文件权限,最初创建为新文件.

现在,一切顺利,保存的文件似乎有模式644.

我需要在这里更改什么才能使文件保存为模式777

万分感谢任何提示,线索或答案.我认为与此相关的代码包括:

/* 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;
}
Run Code Online (Sandbox Code Playgroud)

the*_*ist 25

PHP有一个内置的函数叫做 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;
}
Run Code Online (Sandbox Code Playgroud)

  • _Don't_使用777,这是一个_decimal_数字,没有你喜欢的东西:-) (12认同)

Mic*_*ski 6

您只需手动设置所需的权限chmod():

private function writeFileContent($file, $content){
    $fp = fopen($file, 'w');
    fwrite($fp, $content);
    fclose($fp);

    // Set perms with chmod()
    chmod($file, 0777);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 看到我对科学家的评论 - 正确的面具是**0777**而不是777. (2认同)