PHP 文件操作类(创建文件并写入) 生成日志

<?php
/**
 * 文件操作(生成日志)支持多条插入
 * (如果插入多条语句并换行  用','逗号分开)
 *
 */
class log {
	public $path 	= './info.txt';		//默认值文件
	public $mode 	= 'a';				//默认追加写
	public $content = '默认值:空';			//默认内容是 空
		
	public function addlog($path = null, $mode = null, $content = null) {
		
		//判断写入的文件名是否为空
		if (! empty ( $path )) {
			$this->path = $path;
		}
		
		//判断操作方式 a追加写
		if (! empty ( $mode )) {
			$this->mode = $mode;
		}
		
		//判断写入的内容
		if (! empty ( $content )) {
			$this->content = $content;
		}
		
		$handle = fopen ( $this->path, $this->mode );
		
		//拆分换行
		$string = explode ( ",", $this->content );
		foreach ( $string as $v ) {
			fwrite ( $handle, $v . "\r\n" );
		}
		fclose ( $handle );
	}
}

//使用

$log = new log ();
// $log->addlog ();	//不传值 走默认值
// $log->addlog ( "./log", "a", " 内容1:$content1  内容2: $content2  内容3: $content3 " ); //传多个内容
// $log->addlog ( "./log", "a", "123,123,123" ); //一次插入并换行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值