一个简单的php变量记录/读取类

<?
// lostgdi 56079024@qq.com
class Ini{
var $file_name = ""; //目标文件名
var $save_buffer = ""; //写入的缓冲
var $file_attr_msg = ""; //文件参数信息
// var $load_buffer = ""; //读出的缓冲
var $op = ""; //内部标记,操作方式
var $max_input_date_length = 4000000;//最大写入字节
var $max_output_date_length= 4000000;//最大读取字节
var $error_msg = ""; //错误信息记录--报告
function Ini($file_name=""){
$this->file_name = $file_name;
}

//初始化
function Init(){
if( empty($this->file_name) ){
$this->error_msg = "file name empty";
return false;
}
else if( is_dir($this->file_name) ){
$this->error_msg = "file name can not be dir";
return false;
}
else{
if( $this->op=='read' ){
if( !$this->CheckRead() ){
$this->error_msg = "file do not exist | can not permit to read";
return false;
}
else if( filesize($this->file_name) > $this->max_output_date_length ){
$this->error_msg = "file size more than $this->max_output_date_length , did not permit ";
return false;
}
}
else if( $this->op=='write' ) {
if( !$this->CheckWrite() ){
$this->error_msg = "can not permit to write";
return false;
}
else if( strlen($this->save_buffer) > $this->max_input_date_length ){
$this->error_msg = "save date size more than $this->max_input_date_length , did not permit ";
return false;
}
else if( sizeof($this->save_buffer)==0 || empty($this->save_buffer) ){
$this->error_msg = "no date to save";
return false;
}
}
else if( $this->op=='attribute' ){
if( $this->file_attr_msg!='filesize' || $this->file_attr_msg!='filemtime' ){
$this->error_msg = "query ".$this->file_attr_msg." not support current";
return false;
}
}
return true;
}
}

//输入文件名
function Inifile($file_name){
$this->file_name = $file_name;
}

//检查可读
function CheckRead(){
return is_readable($this->file_name);
}

//检查可写
function CheckWrite(){
if( $this->CheckExist() ) return is_writable($this->file_name);
else return is_writable( dirname($this->file_name) );
}

//检查文件存在
function CheckExist(){
return file_exists($this->file_name);
}

//读出数据
function Load(){
$this->op = "read";
if( !$this->Init() ) return false;

$buffer = "";
$fp = fopen($this->file_name,'r');
while (!feof($fp)) $buffer .= fread($fp, 4096);
fclose($fp);
return unserialize($buffer);
}

//写入数据
function Save($input_buffer=""){
$this->op = "write";
$this->save_buffer = serialize($input_buffer);
if( !$this->Init() ) return false;

$fp = fopen($this->file_name,'w');
fwrite($fp,$this->save_buffer);
fclose($fp);
return true;
}

//返回文件属性
function Attr($par){
$this->op = "attribute";
$this->file_attr_msg = $par;
if( !$this->Init() ) return false;

return $par($this->file_name);
}

//返回错误信息
function GetErrorMsg(){
return $this->error_msg;
}
}//class


/*
require_once('ini.php');
$buffer = 'hihi';
$obj = new Ini('tools/hehe.txt');
$obj->Save($buffer);
echo $obj->GetErrorMsg();
echo $read = $obj->Load();

*/
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值