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

问题描述 投票:0回答:3

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

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

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 file file-permissions chmod
3个回答
34
投票

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

7
投票

您只需使用

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

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

    // Set perms with chmod()
    chmod($file, 0777);
    return true;
}

2
投票

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

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

如果您希望所有新文件都具有某些权限,则需要考虑设置您的

umode
。这是对标准模式应用默认修改的过程设置。

这是一种减法。我的意思是

umode
022
将为您提供默认权限
755
(
777 - 022 = 755
)。

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

© www.soinside.com 2019 - 2024. All rights reserved.