PHP 简单的读写文件

<?php
    class gfile{
        private $path;
        private $fp;
        function __construct(String $filepath){
            if(isset($filepath)){
                if(file_exists($filepath)){
                    $this->path = $filepath;
                }else{
                    throw new Exception("undefind path", 1);
                }
            }else{
                throw new Exception("undefind parm", 1);
            }
        }
        
        public function setVal(String $val){
            $this->fp = fopen($this->path,"w+");
            flock($this->fp, LOCK_EX) ;
            $val ? $val : 0;
            fwrite($this->fp,$val);
        }

        public function getVal(){
            $this->fp = fopen($this->path,"r");
            flock($this->fp, LOCK_EX) ;
            return fread($this->fp, filesize($this->path));
        }

        function __destruct(){
            if($this->fp){
                flock($this->fp, LOCK_UN) ;
                fclose($this->fp);    
            }            
        }
    }
    $gfile = new gfile("lid.txt");
    echo $gfile->getVal();
?>

 

posted @ 2016-01-06 14:59  鱼尾纹  阅读(262)  评论(0编辑  收藏  举报