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 ......
$username = "root";
$password = "123";
// 建立连接
mysql_connect('localhost', $username, $p ......
< ?php
require("mail/class.phpmailer.php");//调用
$mail = new PHPMailer();//实例化phpmailer
$address = "mailxi@126.com";//接收邮件的邮箱
$mail->IsSMTP(); // 设置发送邮件的协议:SMTP
......
由于年前把工作确认下来了,于是准备利用撰写毕业论文的期间学习一下PHP的开发,任何一门语言的学习过程,第一步都是开发环境的成功配置,于是我花了一上午时间把环境搭建成功。
PHP开发环境配置过程详解
本次配置主要针对于Windows XP下的配置安装,至于Linux下的配置安装将在以后给予补充。
一、所需软件
Windows XP ......