dongtao6842 2018-12-01 16:01
浏览 134
已采纳

fopen在php中创建root拥有的文件

I'm writing the code for converting disqus comments to HashOver system, and I have code like this:

// $threads is list of threads from disqus api that I cached on disk
foreach ($threads as $thread) {
    $dir_name = getSafeThreadName(preg_replace("%^https?://[^/]+%", "", $thread['link']));
    $dir = 'threads/' . $dir_name;
    if (!is_dir($dir)) {
        mkdir($dir);
        chmod($dir, 0774);
    }
    // ...
    // $thread_posts are $post that belong to $thread
    foreach($thread_posts as $post) {
        $xml = new SimpleXMLElement('<comment/>');
        // ...
        // $name_arr is array of numbers for HashOver comments
        $fname = $dir . '/' . implode('-', $name_arr) . ".xml";
        $f = fopen($fname, 'w');
        fwrite($f, $xml->asXML());
        fclose($f);
        chmod($fname, 0664);
    }
}

it created directory for each of my posts in threads that that is read/write with owner apache:apache and inside there are files like 1.xml with owner root:root

why root:root? How can I make it apache:apache?

EDIT:

This is no a duplicate, I don't want to change permission to apache:apache from root:root, which can be done using chown, but I want to make it so it don't change it to root:root in first place, I also want to know why it changed to root:root. This looks like a bug in php or apache for me or some wrong configuration in apache on my side. I don't think is the code since it just open, write and close file.

  • 写回答

1条回答 默认 最新

  • douwei1904 2018-12-01 16:42
    关注

    Don't know why and I would like to know, but this solve the issue:

    I've use this:

    chmod($dir, 0775);
    

    instead of:

    chmod($dir, 0774);
    

    directory was not executable for others and it make files in that directory owned by root when created. Very weird.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿