PHP: 使用fopen进行文件读写

Categories: PHP; Tagged with: ; @ August 28th, 2010 11:48

1. 按行读取数据:

		// 读取上次报错时间
		try {
			$fhRead =  @fopen("timestamp/$this->id.txt", "r");
			if($fhRead) {
				// echo "时间记录文件第一行".fgets($fhRead)."
"; // echo "时间记录文件第二行".fgets($fhRead)."
"; $lastSendEmail = strtotime(fgets($fhRead)); $lastSendSms = strtotime(fgets($fhRead)); }else { $this->logMesg("文件有误, 无法读取"); } }catch (Exception $e) { $this->logMesg("读取报错时间记录文件遇到错误:".$e); }

2. 写入数据:

		// 记录报错时间.
		try {
			$fhMarkTime = @fopen("timestamp/$this->id.txt", "w");
			if($fhMarkTime) {
				fwrite($fhMarkTime, date("Y-m-d H:i:s",$upadtedLastSendEmail)."\n");
				fwrite($fhMarkTime, date("Y-m-d H:i:s",$updatedLastSendSms));
			}else {
				echo "文件写入错误.";
			}
		}catch (Exception $e) {
			echo "文件写入错误: ".$e;
		}

PHP Array 简单操作小结

Categories: PHP; Tagged with: ; @ August 28th, 2010 11:32

1. 创建Array: $arraySites = array();

2. addItem: $arraySites[$site->id] = $site;

3. 使用foreach遍历Array:

foreach ($arraySites as $site) { // 遍历每一个Site.
	// do something.
}

// Or:
foreach($array as $key=>$value)
{
echo "$key,$value ";
}

4. length: count($arraySite);

5.  打散/切割 Array : array_chunk($mails, $MAX_MAILS_PER_SMTP_SESSION, true);

该方法将会根据设定的大小对原数组进行切割, 生成多个小的数组(最后一个可能会少于单元大小), 第三个参数控制是否在打散后的数组内保留其原有key.

复制/移除 数组: http://liguoliang.com/2010/copy-and-remove-array-to-array-in-php/

PHP String 简单操作总结

Categories: PHP; Tagged with: ; @ August 28th, 2010 11:23

1. String 长度: strlen($this->notifyEmailTo)

2. Trim: trim($str); // 清除\0, \t, \n等字符

3. Split: explode(",", $str); // 以","隔开的string组成的数组

4. IndexOf:  strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

5. Replace: str_replace("{ID}", $this->id, "ServerID: {ID}");

补充:

indexOf:  $indexOfAt = strrpos($mailAdd, "@");

SubString: $userName = substr($mailAdd, 0, $indexOfAt);

使用PHP运行Shell命令 – PHP Ajax Shell

Categories: LinuxPHP; Tagged with: ; @ August 17th, 2010 23:07

对于PHP的虚拟主机, 有时需要查看系统配置或是运行其他的Shell命令,

网上有很多类似的代码, 一般来说很短一个PHP文件即可运行Shell命令, 便于更方便的管理虚拟主机.

image (more…)

WordPress Debug – 查看Query执行的SQL语句

Categories: Development NotesPHPWordPress; Tagged with: ; @ July 20th, 2010 8:58

有时Query不到东西, 怀疑SQL执行有问题时, 可在Query.php适当位置加入Print进行Debug.

(more…)

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.