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);
相关文档:
AddSlashes: 字符串加入斜线。
bin2hex: 二进位转成十六进位。
Chop: 去除连续空白。
Chr: 返回序数值的字符。
chunk_split: 将字符串分成小段。
convert_cyr_string: 转换古斯拉夫字符串成其它字符串。
crypt: 将字符串用 DES 编码加密。
echo: 输出字符串。
explode: 切开字符串。
flush: 清出输出缓冲区。
get ......
和很多语言不同,在PHP中使用变量之前不需要声明,只需要为变量赋值即可,PHP中的变量名称用$和标识符表示,变量名是区别大小写的。
变量赋值,是指给变量一个具体的数据数据值,对于字符串和数字类型的变量,可以通过"="来实现。
除了直接赋值外,还有两种方式来给变量声明或赋值。一种是变量间的赋值。另一种是引用赋值。 ......
eader()函数使用说明:
一、作用:
~~~~~~~~~
PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了,可参照http://www.w3.org/Protocols/rfc2616/rfc2616。
& ......
对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用。
由于 Session 是以文本文件形式存储在服务器端的,所以不怕客户端修改 Session 内容。实际上在服务器端的 Session 文件,PHP 自动修改 Session 文件的权限,只保留了系统读和写权 ......
- -! 真不容易啊,弄了一天,终于搞定了!
在ubuntu9.04下安装,首先要准备的东西很多,大部分是用来支持php的库,废话不多说,写这个也是为了便于我以后查看用,记忆不行,咳。。有需要的朋友也可以来去用。
在ubuntu上安装,有一个好处就是有新立得这个软件,首先先安装mysql, ......