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


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

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

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

为了使文件保存为模式777,我必须在这里更改什么?

感谢您的任何提示、线索或答案。我认为相关的代码在这里我已经包括:

/* write to file */
   self::writeFileContent($path, $value);
/* Write content to file
* @param string $file   Save content to which 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;
}

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;
}

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

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

如果您想更改现有文件的权限,请使用chmod(更改模式):

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

如果您希望所有新文件都具有特定权限,则需要考虑设置umode。这是一个将默认修改应用于标准模式的过程设置。

这是一个减法。我的意思是,022umode将为您提供755777 - 022 = 755)的默认权限。

但是您应该仔细考虑这两个选项。使用该模式创建的文件将完全不受更改的保护。