• 技术文章 >后端开发 >php教程

    php 多线程上下文中安全写文件实现代码_php技巧

    2016-05-17 09:28:42原创288
    复制代码 代码如下:

    /**
    * @usage: used to offer safe file write operation in multiple threads context, arbitory file type
    * @author: Rocky Zhang
    * @time: Nov. 11 2009
    * @demo[0]: $handler = mfopen($file, 'a+');
    * mfwrite($handler, $str);
    */
    function mfopen($file, $mode='w+') {
    $tempfile = generateTempfile('./tempdir', $file);
    preg_match('/b/i', $mode) || ($mode .= 'b'); // 'b' is recommended
    if (preg_match('/\w|a/i', $mode) && !is_writable($file)) {
    exit("{$file} is not writable!");
    }
    $filemtime = $filemtime2 = 0;
    $tempdir = dirname($tempfile);
    is_dir($tempdir) || mkdir($tempdir, 0777);
    do { // do-while used to avoid modify in a long time copy
    clearstatcache();
    $filemtime = filemtime($file);
    copy($file, $tempfile);
    $filemtime2 = filemtime($file);
    } while ( ($filemtime2 - $filemtime) != 0 );
    if (!$handler = fopen($tempfile, $mode)) {
    exit('Fail on opening tempfile, write authentication is must on temporary dir!');
    }
    return array(0=>$handler, 1=>$filemtime, 2=>$file, 3=>$tempfile, 4=>$mode);
    }

    // I do think that this function should be optimized further
    function mfwrite(&$handler, $str='') {
    if (strlen($str) > 0) {
    $num = fwrite($handler[0], $str);
    fflush($handler[0]);
    }
    clearstatcache();
    $mtime = filemtime($handler[2]);
    if ( $mtime == $handler[1] ) { // compare between source file and temporary file
    if ( $num && $num > 0 ) { // temporary file has been updated, copy to source file
    copy($handler[3], $handler[2]) || exit;
    $handler[1] = filemtime($handler[3]);
    touch($handler[2], $handler[1], $handler[1]);
    }
    } else { // source file has been modified, load source file to temporary file
    copy($handler[2], $handler[3]) || exit;
    touch($handler[3], $mtime, $mtime);
    $handler[1] = $mtime;
    }
    }

    function generateTempfile($tempdir='tempdir', $file) {
    $rand = md5(microtime());
    return "{$tempdir}/{$rand}_".$file;
    }
    ?>
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php 无极分类(递归)实现代码_php技巧 下一篇:php 文件上传代码(限制jpg文件)_php技巧
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 整理总结nginx、php-fpm和mysql等的权限划分• 分享自定义的几个PHP功能函数• php常见的页面跳转方法汇总_PHP教程• ThinkPHP 404页面的设置方法_PHP教程• PHP多个文件上传到服务器实例,_PHP教程
    1/1

    PHP中文网