fopen创建文件,但fwrite不写任何数据并返回0

时间:2014-04-21 13:35:57

标签: php linux openx

以下是OpenX服务器代码(Smarty插件)的一部分。 调用fopen时,将成功创建文件。但是fwrite不会在文件中写入任何数据并返回0(不是FALSE)。

文件夹本身具有以下权限: drwxrwxrwx没人没人

使用以下权限创建文件: -rw-r - r--没人没人

这是代码。

function smarty_core_write_file($params, &$smarty)
{
    $_dirname = dirname($params['filename']);

    if ($params['create_dirs']) {
        $_params = array('dir' => $_dirname);
        require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
        smarty_core_create_dir_structure($_params, $smarty);
    }

    // write to tmp file, then rename it to avoid file locking race condition
    $_tmp_file = tempnam($_dirname, 'wrt');

    if (!($fd = fopen($_tmp_file, 'wb'))) {
        $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
        if (!($fd = fopen($_tmp_file, 'wb'))) {
            $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
            return false;
        }
    }

    fwrite($fd, $params['contents']);
    fclose($fd);

    if (DIRECTORY_SEPARATOR == '\\' || !rename($_tmp_file, $params['filename'])) {
        // On platforms and filesystems that cannot overwrite with rename() 
        // delete the file before renaming it -- because windows always suffers
        // this, it is short-circuited to avoid the initial rename() attempt
        unlink($params['filename']);
        rename($_tmp_file, $params['filename']);
    }
    chmod($params['filename'], $smarty->_file_perms);

    return true;
}

以下是我用来测试此功能的代码:

$params = array('filename'=>'/usr/apache/www/templates_compiled/%%72^729^729250E1%%main.html.php', 'contents'=>'testString', 'create_dirs'=>TRUE);
smarty_core_write_file($params, NULL);

1 个答案:

答案 0 :(得分:1)

问题是服务器已满。因此,在释放一些空间后,代码运行良好。