文件删除和CHMOD:如何在PHP中设置777创建文件?

hrirmatl  于 5个月前  发布在  PHP
关注(0)|答案(3)|浏览(62)

当保存一个不存在的文件时,有关文件权限的问题,最初创建为新文件。
现在,一切都很顺利,保存的文件似乎具有模式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;
}

字符串

7y4bm7vi

7y4bm7vi1#

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

字符串

aiqt4smr

aiqt4smr2#

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

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

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

字符串

irlmq6kh

irlmq6kh3#

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

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

字符串
如果你想让所有的新文件都有一定的权限,你需要设置你的umode。这是一个进程设置,它将默认修改应用到标准模式。
这是一个减法。我的意思是022umode将给予您755777 - 022 = 755)的默认权限。
但是你应该非常仔细地考虑这两个选项。用这种模式创建的文件将完全不受保护。

相关问题