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;
		}



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