File.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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,0755,true);
  42. if(false === file_put_contents($filename,$content)){
  43. E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
  44. }else{
  45. $this->contents[$filename]=$content;
  46. return true;
  47. }
  48. }
  49. /**
  50. * 文件追加写入
  51. * @access public
  52. * @param string $filename 文件名
  53. * @param string $content 追加的文件内容
  54. * @return boolean
  55. */
  56. public function append($filename,$content,$type=''){
  57. if(is_file($filename)){
  58. $content = $this->read($filename,$type).$content;
  59. }
  60. return $this->put($filename,$content,$type);
  61. }
  62. /**
  63. * 加载文件
  64. * @access public
  65. * @param string $filename 文件名
  66. * @param array $vars 传入变量
  67. * @return void
  68. */
  69. public function load($_filename,$vars=null){
  70. if(!is_null($vars))
  71. extract($vars, EXTR_OVERWRITE);
  72. include $_filename;
  73. }
  74. /**
  75. * 文件是否存在
  76. * @access public
  77. * @param string $filename 文件名
  78. * @return boolean
  79. */
  80. public function has($filename,$type=''){
  81. return is_file($filename);
  82. }
  83. /**
  84. * 文件删除
  85. * @access public
  86. * @param string $filename 文件名
  87. * @return boolean
  88. */
  89. public function unlink($filename,$type=''){
  90. unset($this->contents[$filename]);
  91. return is_file($filename) ? unlink($filename) : false;
  92. }
  93. /**
  94. * 读取文件信息
  95. * @access public
  96. * @param string $filename 文件名
  97. * @param string $name 信息名 mtime或者content
  98. * @return boolean
  99. */
  100. public function get($filename,$name,$type=''){
  101. if(!isset($this->contents[$filename])){
  102. if(!is_file($filename)) return false;
  103. $this->contents[$filename]=file_get_contents($filename);
  104. }
  105. $content=$this->contents[$filename];
  106. $info = array(
  107. 'mtime' => filemtime($filename),
  108. 'content' => $content
  109. );
  110. return $info[$name];
  111. }
  112. }