分享

PHP 无数据库读写配置文件 | 孟坤博客

 黄三岁大爱人生 2018-03-18

做一个动态网页,一般都会有各种后台配置。如果只是少量的配置数据,单独弄个数据库来进行存储就显得有些“浪费”了。

下面分享一个自用的无数据库配置文件增删查改模块,可以很方便地存储读取数据。有需要的就拿去用吧

食用方法

新建一个 PHP 文件,命名为 Config.class.php,然后按照注释中的内容来使用就行~

  1. <?php  
  2.   
  3. /* 
  4. * @link https:/// 
  5. * @author mengkun 
  6. * @license MIT 
  7. */  
  8.   
  9. /** 
  10.  * PHP 无数据库配置文件增删查改模块 
  11.  * !注:本模块未对高并发进行优化兼容,如果数据量或并发过大,还是用数据库比较好 🙂 
  12.  *  
  13.  * 使用方法: 
  14.  *  
  15.  * 一、引用本模块 
  16.  *  
  17.  *  require_once 'Config.class.php'; 
  18.  *  
  19.  *  
  20.  * 二、初始化 
  21.  *  
  22.  *  $C = new Config('配置文件名');  // * 如果是在二级目录下,请确保该目录存在 
  23.  *  
  24.  *  
  25.  * 三、内置方法 
  26.  *  
  27.  *  - 存储(如果已存在则是修改)单条数据 
  28.  *  
  29.  *      $C->set('sitename', '孟坤博客'); 
  30.  *  
  31.  *  
  32.  *  - 存储(如果已存在则是修改)一个数组 
  33.  *       
  34.  *      $C->set('user', array('name'=>'peter', 'age'=>12)); 
  35.  *  
  36.  *  
  37.  *  - 读取一条数据 
  38.  *  
  39.  *      $C->get('user', '默认值'); 
  40.  *  
  41.  *  
  42.  *  - 删除一条数据 
  43.  *  
  44.  *      $C->delete('user'); 
  45.  *  
  46.  *  
  47.  *  - 保存对数据的修改 
  48.  *  
  49.  *      $C->save();     // 保存成功返回 true,否则返回失败原因 
  50.  *  
  51.  *   
  52.  * * 注:为了避免频繁地写文件,以上所有对数据的操作都必须调用一次 $C->save(); 才会真正被保存到配置文件中 
  53.  *       建议将所有的数据操作都执行完后再进行存储操作。 
  54.  *  
  55.  *  
  56.  * * 附:精简写法 
  57.  *  
  58.  *      $C->set('sitename', '孟坤博客')->save(); 
  59.  */  
  60.   
  61. define('CONFIG_EXIT', '<?php exit;?>');  
  62. class Config {  
  63.     private $data;  
  64.     private $file;  
  65.       
  66.     /** 
  67.      * 构造函数 
  68.      * @param $file 存储数据文件 
  69.      * @return  
  70.      */  
  71.     function __construct($file) {  
  72.         $file = $file.'.php';  
  73.         $this->file = $file;  
  74.         $this->data= self::read($file);  
  75.     }  
  76.       
  77.     /** 
  78.      * 读取配置文件 
  79.      * @param $file 要读取的数据文件 
  80.      * @return 读取到的全部数据信息 
  81.      */  
  82.     public function read($file) {  
  83.         if(!file_exists($file)) return array();  
  84.           
  85.         $str = file_get_contents($file);  
  86.         $str = substr($str, strlen(CONFIG_EXIT));  
  87.         $data = json_decode($str, true);  
  88.         if (is_null($data)) return array();  
  89.         return $data;  
  90.     }  
  91.       
  92.     /** 
  93.      * 获取指定项的值 
  94.      * @param $key 要获取的项名 
  95.      * @param $default 默认值 
  96.      * @return data 
  97.      */  
  98.     public function get($key = null, $default = '') {  
  99.         if (is_null($key)) return $this->data;  // 取全部数据  
  100.           
  101.         if(isset($this->data[$key])) return $this->data[$key];  
  102.         return $default;  
  103.     }  
  104.       
  105.     /** 
  106.      * 设置指定项的值 
  107.      * @param $key 要设置的项名 
  108.      * @param $value 值 
  109.      * @return null 
  110.      */  
  111.     public function set($key, $value) {  
  112.         if(is_string($key)) {   // 更新单条数据  
  113.             $this->data[$key] = $value;  
  114.         } else if(is_array($key)) {   // 更新多条数据                 
  115.             foreach ($this->data as $k => $v) {  
  116.                 if ($v[$key[0]] == $key[1]) {  
  117.                     $this->data[$k][$value[0]] = $value[1];  
  118.                 }  
  119.             }  
  120.         }  
  121.           
  122.         return $this;  
  123.     }  
  124.       
  125.     /** 
  126.      * 删除并清空指定项 
  127.      * @param $key 删除项名 
  128.      * @return null 
  129.      */  
  130.     public function delete($key) {  
  131.         unset($this->data[$key]);  
  132.           
  133.         return $this;  
  134.     }  
  135.       
  136.     /** 
  137.      * 保存配置文件 
  138.      * @param $file 要保存的数据文件 
  139.      * @return true-成功 其它-保存失败原因 
  140.      */  
  141.     public function save() {  
  142.         if(defined('JSON_PRETTY_PRINT')) {  
  143.             $jsonStr = json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);  
  144.         } else {  
  145.             $jsonStr = json_encode($this->data);  
  146.         }  
  147.           
  148.         // 含有二进制或非utf8字符串对应检测  
  149.         if(is_null($jsonStr)) return '数据文件有误';  
  150.         $buffer = CONFIG_EXIT.$jsonStr;  
  151.           
  152.         $file_strm = fopen($this->file, 'w');  
  153.         if(!$file_strm) return '写入文件失败,请赋予 '.$file.' 文件写权限!';  
  154.         fwrite($file_strm, $buffer);  
  155.         fclose($file_strm);  
  156.         return true;  
  157.     }  
  158. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多