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);
相关文档:
// 定义一个新变量
$test = "hello";
// . 字符串连接符
echo $test.".world" // hello.world
echo "$test.world" // "" 中的变量将被解析成相应的值
&nbs ......
我们知道在PHP4里面,当我们使用一个类的时候,必须将该类所以文件使用include或require加进来,这样我们就可以使用类了。
而对于PHP5来说也可以使用在PHP4里面的方式,将类文件包进来,但当有了__autoload之后,这种情况就可以靠编写规则也实现。
以下代码:
<?php
$obj_A = new clsA();
......
和很多语言不同,在PHP中使用变量之前不需要声明,只需要为变量赋值即可,PHP中的变量名称用$和标识符表示,变量名是区别大小写的。
变量赋值,是指给变量一个具体的数据数据值,对于字符串和数字类型的变量,可以通过"="来实现。
除了直接赋值外,还有两种方式来给变量声明或赋值。一种是变量间的赋值。另一种是引用赋值。 ......
- -! 真不容易啊,弄了一天,终于搞定了!
在ubuntu9.04下安装,首先要准备的东西很多,大部分是用来支持php的库,废话不多说,写这个也是为了便于我以后查看用,记忆不行,咳。。有需要的朋友也可以来去用。
在ubuntu上安装,有一个好处就是有新立得这个软件,首先先安装mysql, ......
--------------
安装pear
--------------
pear是PHP的扩展和应用程序库,包含了很多有用的类,安装好php5.0后,pear实际上并没有被安装,安装的方法如下:
1.在php目录中双击go-pear.bat。
2.按照提示输入一些设置信息, ......