douaoren4402
2011-06-22 00:39
浏览 310
已采纳

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

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.

What to I have to change here, in order to make the files save as mode 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;
}

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

  / *写入文件* / 
 
 self :: writeFileContent($ path,$ value); \  n 
 / *将内容写入文件
 * @param string $ file将内容保存到wich文件
 * @param string $ content需要写入文件的字符串
 * @return bool 
 * / \  n 
private函数writeFileContent($ file,$ content){
 $ fp = fopen($ file,'w'); 
 fwrite($ fp,$ content); 
 fclose($ fp); 
 return 是; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushenmao9036 2011-06-22 00:41
    已采纳

    PHP has a built in function called 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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxia9620 2011-06-22 00:42

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

    private function writeFileContent($file, $content){
        $fp = fopen($file, 'w');
        fwrite($fp, $content);
        fclose($fp);
    
        // Set perms with chmod()
        chmod($file, 0777);
        return true;
    }
    
    评论
    解决 无用
    打赏 举报
  • duanhao4909 2011-06-22 00:45

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

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

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

    But you should think very carefully about both these options. Files created with that mode will be totally unprotected from changes.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题