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


Posted in PHP onDecember 28, 2009
<?php 
/** 
* @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; 
} 
?>
PHP 相关文章推荐
用PHP ob_start()控制浏览器cache、生成html实现代码
Feb 16 PHP
PHP实现域名whois查询的代码(数据源万网、新网)
Feb 22 PHP
PHP代码保护--Zend Guard的使用详解
Jun 03 PHP
PHP JS Ip地址及域名格式检测代码
Sep 27 PHP
php上传图片到指定位置路径保存到数据库的具体实现
Dec 30 PHP
thinkphp实现图片上传功能分享
Mar 04 PHP
PHP实现可添加水印与生成缩略图的图片处理工具类
Jan 16 PHP
thinkphp5 migrate数据库迁移工具
Feb 20 PHP
PHPUnit测试私有属性和方法功能示例
Jun 12 PHP
asp函数split()对应php函数explode()
Feb 27 PHP
laravel 修改.htaccess文件 重定向public的解决方法
Oct 12 PHP
php使用event扩展的io复用测试的示例
Oct 20 PHP
PHP 获取目录下的图片并随机显示的代码
Dec 28 #PHP
phpMyAdmin链接MySql错误 个人解决方案
Dec 28 #PHP
php 需要掌握的东西 不做浮躁的人
Dec 28 #PHP
php 文章采集正则代码
Dec 28 #PHP
PHP array_push 数组函数
Dec 26 #PHP
PHP simple_html_dom.php+正则 采集文章代码
Dec 24 #PHP
在PHP中检查PHP文件是否有语法错误的方法
Dec 23 #PHP
You might like
php数组合并array_merge()函数使用注意事项
2014/06/19 PHP
php生成无限栏目树
2017/03/16 PHP
基于php中echo用逗号和用点号的区别详解
2018/01/23 PHP
PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能
2018/05/31 PHP
PHP设计模式之PHP迭代器模式讲解
2019/03/22 PHP
基于JQuery 选择器使用说明介绍
2013/04/18 Javascript
鼠标经过tr时,改变tr当前背景颜色
2014/01/13 Javascript
JS+CSS实现感应鼠标渐变显示DIV层的方法
2015/02/20 Javascript
js断点调试心得分享(必看篇)
2017/12/08 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
vue设置一开始进入的页面教程
2019/10/28 Javascript
[02:05]2014DOTA2西雅图国际邀请赛 BBC第二天小组赛总结
2014/07/11 DOTA
[07:54]DOTA2-DPC中国联赛 正赛 iG vs VG 选手采访
2021/03/11 DOTA
Python中用Descriptor实现类级属性(Property)详解
2014/09/18 Python
python中函数总结之装饰器闭包详解
2016/06/12 Python
在Linux命令行终端中使用python的简单方法(推荐)
2017/01/23 Python
Python学习小技巧之利用字典的默认行为
2017/05/20 Python
Python IDLE入门简介
2017/12/08 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
Django实现任意文件上传(最简单的方法)
2020/06/03 Python
Python 中的函数装饰器和闭包详解
2021/02/06 Python
加拿大花店:1800Flowers.ca
2016/11/16 全球购物
bareMinerals官网:矿物质化妆品和护肤品
2018/02/04 全球购物
德国消费电子产品购物网站:Guter Kauf
2020/09/15 全球购物
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
internal修饰符起什么作用
2013/12/16 面试题
运动会领导邀请函
2014/01/10 职场文书
总经理文秘岗位职责
2014/02/03 职场文书
2014年秋季开学典礼致辞
2014/08/02 职场文书
2014年班组长工作总结
2014/11/20 职场文书
司机个人年终总结
2015/03/03 职场文书
自荐信模板大全
2015/03/27 职场文书
教师教育心得体会
2016/01/19 职场文书
奖学金申请书(范文)
2019/08/14 职场文书
Java实现给Word文件添加文字水印
2022/02/15 Java/Android
排查并解决Oracle sysaux表空间异常增长
2022/04/20 Oracle