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

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.

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

报告相同问题?