码迷,mamicode.com
首页 > Web开发 > 详细

PHP 简单的读写文件

时间:2016-01-06 15:39:07      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

<?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();
?>

 

PHP 简单的读写文件

标签:

原文地址:http://www.cnblogs.com/bin-pureLife/p/5105640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!