You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.4 KiB

3 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Storage\Driver;
  12. use Think\Storage;
  13. // 本地文件写入存储类
  14. class File extends Storage{
  15. private $contents=array();
  16. /**
  17. * 架构函数
  18. * @access public
  19. */
  20. public function __construct() {
  21. }
  22. /**
  23. * 文件内容读取
  24. * @access public
  25. * @param string $filename 文件名
  26. * @return string
  27. */
  28. public function read($filename,$type=''){
  29. return $this->get($filename,'content',$type);
  30. }
  31. /**
  32. * 文件写入
  33. * @access public
  34. * @param string $filename 文件名
  35. * @param string $content 文件内容
  36. * @return boolean
  37. */
  38. public function put($filename,$content,$type=''){
  39. $dir = dirname($filename);
  40. if(!is_dir($dir)){
  41. mkdir($dir,0777,true);
  42. }
  43. if(false === file_put_contents($filename,$content)){
  44. E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
  45. }else{
  46. $this->contents[$filename]=$content;
  47. return true;
  48. }
  49. }
  50. /**
  51. * 文件追加写入
  52. * @access public
  53. * @param string $filename 文件名
  54. * @param string $content 追加的文件内容
  55. * @return boolean
  56. */
  57. public function append($filename,$content,$type=''){
  58. if(is_file($filename)){
  59. $content = $this->read($filename,$type).$content;
  60. }
  61. return $this->put($filename,$content,$type);
  62. }
  63. /**
  64. * 加载文件
  65. * @access public
  66. * @param string $filename 文件名
  67. * @param array $vars 传入变量
  68. * @return void
  69. */
  70. public function load($_filename,$vars=null){
  71. if(!is_null($vars)){
  72. extract($vars, EXTR_OVERWRITE);
  73. }
  74. include $_filename;
  75. }
  76. /**
  77. * 文件是否存在
  78. * @access public
  79. * @param string $filename 文件名
  80. * @return boolean
  81. */
  82. public function has($filename,$type=''){
  83. return is_file($filename);
  84. }
  85. /**
  86. * 文件删除
  87. * @access public
  88. * @param string $filename 文件名
  89. * @return boolean
  90. */
  91. public function unlink($filename,$type=''){
  92. unset($this->contents[$filename]);
  93. return is_file($filename) ? unlink($filename) : false;
  94. }
  95. /**
  96. * 读取文件信息
  97. * @access public
  98. * @param string $filename 文件名
  99. * @param string $name 信息名 mtime或者content
  100. * @return boolean
  101. */
  102. public function get($filename,$name,$type=''){
  103. if(!isset($this->contents[$filename])){
  104. if(!is_file($filename)) return false;
  105. $this->contents[$filename]=file_get_contents($filename);
  106. }
  107. $content=$this->contents[$filename];
  108. $info = array(
  109. 'mtime' => filemtime($filename),
  110. 'content' => $content
  111. );
  112. return $info[$name];
  113. }
  114. }