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语法(1)
Oct 09 PHP
PHP内核介绍及扩展开发指南―基础知识
Sep 11 PHP
Windows中使用计划任务自动执行PHP程序实例
May 09 PHP
php+ajax实现无刷新分页的方法
Nov 04 PHP
Yii把CGridView文本框换成下拉框的方法
Dec 03 PHP
php读取远程gzip压缩网页的方法
Dec 29 PHP
php跨服务器访问方法小结
May 12 PHP
PHP变量赋值、代入给JavaScript中的变量
Jun 29 PHP
php根据年月获取当月天数及日期数组的方法
Nov 30 PHP
php抽象方法和抽象类实例分析
Dec 07 PHP
Yii框架弹出窗口组件CJuiDialog用法分析
Jan 07 PHP
php+resumablejs实现的分块上传 断点续传功能示例
Apr 18 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时间不正确的解决方法
2008/04/09 PHP
PHP微信API接口类
2016/08/22 PHP
PHP内存缓存功能memcached示例
2016/10/19 PHP
php输出控制函数和输出函数生成静态页面
2019/06/27 PHP
JQuery Easyui Tree的oncheck事件实现代码
2010/05/28 Javascript
基于jQuery实现表格数据的动态添加与统计的代码
2011/01/31 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
使用原生js写的一个简单slider
2014/04/29 Javascript
浅谈node.js中async异步编程
2015/10/22 Javascript
angularJS 指令封装回到顶部示例详解
2017/01/22 Javascript
vue中页面跳转拦截器的实现方法
2017/08/23 Javascript
vue实现a标签点击高亮方法
2018/03/17 Javascript
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2018/05/15 Javascript
在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
2015/04/25 Python
Python中用altzone()方法处理时区的教程
2015/05/22 Python
Python2.7读取PDF文件的方法示例
2017/07/13 Python
详解supervisor使用教程
2017/11/21 Python
Python实现PS图像调整颜色梯度效果示例
2018/01/25 Python
解决PyCharm控制台输出乱码的问题
2019/01/16 Python
python3实现mysql导出excel的方法
2019/07/31 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
python 实现两个线程交替执行
2020/05/02 Python
Django之富文本(获取内容,设置内容方式)
2020/05/21 Python
Python txt文件常用读写操作代码实例
2020/08/03 Python
python爬虫中PhantomJS加载页面的实例方法
2020/11/12 Python
解决python3中os.popen()出错的问题
2020/11/19 Python
python脚本使用阿里云slb对恶意攻击进行封堵的实现
2021/02/04 Python
python 爬取腾讯视频评论的实现步骤
2021/02/18 Python
a标签下载链接的简单实现
2016/09/13 HTML / CSS
大学团支书的自我评价分享
2013/12/14 职场文书
暑期社会实践学生的自我评价
2014/01/09 职场文书
应届生求职自荐信范文
2015/03/04 职场文书
2015年小学生国庆节演讲稿
2015/07/30 职场文书
2016党员学习心得体会范文
2016/01/23 职场文书
Python虚拟环境virtualenv是如何使用的
2021/06/20 Python
对象析构函数__del__在Python中何时使用
2022/03/22 Python