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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6433643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:22:53  来源:igfitidea点击:

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

phpfilefile-permissionschmod

提问by Sam

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.

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

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

为了使文件另存为模式,我必须在这里更改什么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;
}

回答by thescientist

PHP has a built in function called bool chmod(string $filename, int $mode )

PHP 有一个内置函数叫做 bool chmod(string $filename, int $mode )

http://php.net/function.chmod

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

回答by Michael Berkowski

You just need to manually set the desired permissions with chmod():

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

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

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

回答by paxdiablo

If you want to change the permissions of an existing file, use chmod(change mode):

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

$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.

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

It is a subtractive one. By that, I mean a umodeof 022will give you a default permission of 755(777 - 022 = 755).

这是一个减法。我的意思是 a umodeof022会给你默认的权限755( 777 - 022 = 755)。

But you should think verycarefully about boththese options. Files created with that mode will be totally unprotected from changes.

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