php多线程上下文中安全写文件
提供一个php多线程上下文中安全写文件的实现方法。这个实现没有使用php 的file lock机制,使用的是临时文件机制。多线程中的各个线程都是对各自(每个线程独占一个)的临时文件写,然后再同步到原文件中。
<?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);
相关文档:
和很多语言不同,在PHP中使用变量之前不需要声明,只需要为变量赋值即可,PHP中的变量名称用$和标识符表示,变量名是区别大小写的。
变量赋值,是指给变量一个具体的数据数据值,对于字符串和数字类型的变量,可以通过"="来实现。
除了直接赋值外,还有两种方式来给变量声明或赋值。一种是变量间的赋值。另一种是引用赋值。 ......
eader()函数使用说明:
一、作用:
~~~~~~~~~
PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了,可参照http://www.w3.org/Protocols/rfc2616/rfc2616。
& ......
APC是一种php的缓存解决方案,目前以pecl方式发布,有消息说将会出现在php6版本的内核.
一.安装方法
1)从http://pecl.php.net/package/apc下载相应版本
2)解压
3)进入源码目录
4)执行php安装目录下的bin/phpize
5)./configure --enable-apc --enable-apc-mmap --with-apxs=path-to-apache/bin/apxs --with-php-config=p ......
由于年前把工作确认下来了,于是准备利用撰写毕业论文的期间学习一下PHP的开发,任何一门语言的学习过程,第一步都是开发环境的成功配置,于是我花了一上午时间把环境搭建成功。
PHP开发环境配置过程详解
本次配置主要针对于Windows XP下的配置安装,至于Linux下的配置安装将在以后给予补充。
一、所需软件
Windows XP ......