File.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. {
  16. private $contents = array();
  17. /**
  18. * 架构函数
  19. * @access public
  20. */
  21. public function __construct()
  22. {
  23. }
  24. /**
  25. * 文件内容读取
  26. * @access public
  27. * @param string $filename 文件名
  28. * @return string
  29. */
  30. public function read($filename, $type = '')
  31. {
  32. return $this->get($filename, 'content', $type);
  33. }
  34. /**
  35. * 文件写入
  36. * @access public
  37. * @param string $filename 文件名
  38. * @param string $content 文件内容
  39. * @return boolean
  40. */
  41. public function put($filename, $content, $type = '')
  42. {
  43. $dir = dirname($filename);
  44. if (!is_dir($dir)) {
  45. mkdir($dir, 0777, true);
  46. }
  47. if (false === file_put_contents($filename, $content)) {
  48. E(L('_STORAGE_WRITE_ERROR_') . ':' . $filename);
  49. } else {
  50. $this->contents[$filename] = $content;
  51. return true;
  52. }
  53. }
  54. /**
  55. * 文件追加写入
  56. * @access public
  57. * @param string $filename 文件名
  58. * @param string $content 追加的文件内容
  59. * @return boolean
  60. */
  61. public function append($filename, $content, $type = '')
  62. {
  63. if (is_file($filename)) {
  64. $content = $this->read($filename, $type) . $content;
  65. }
  66. return $this->put($filename, $content, $type);
  67. }
  68. /**
  69. * 加载文件
  70. * @access public
  71. * @param string $filename 文件名
  72. * @param array $vars 传入变量
  73. * @return void
  74. */
  75. public function load($_filename, $vars = null)
  76. {
  77. if (!is_null($vars)) {
  78. extract($vars, EXTR_OVERWRITE);
  79. }
  80. include $_filename;
  81. }
  82. /**
  83. * 文件是否存在
  84. * @access public
  85. * @param string $filename 文件名
  86. * @return boolean
  87. */
  88. public function has($filename, $type = '')
  89. {
  90. return is_file($filename);
  91. }
  92. /**
  93. * 文件删除
  94. * @access public
  95. * @param string $filename 文件名
  96. * @return boolean
  97. */
  98. public function unlink($filename, $type = '')
  99. {
  100. unset($this->contents[$filename]);
  101. return is_file($filename) ? unlink($filename) : false;
  102. }
  103. /**
  104. * 读取文件信息
  105. * @access public
  106. * @param string $filename 文件名
  107. * @param string $name 信息名 mtime或者content
  108. * @return boolean
  109. */
  110. public function get($filename, $name, $type = '')
  111. {
  112. if (!isset($this->contents[$filename])) {
  113. if (!is_file($filename)) {
  114. return false;
  115. }
  116. $this->contents[$filename] = file_get_contents($filename);
  117. }
  118. $content = $this->contents[$filename];
  119. $info = array(
  120. 'mtime' => filemtime($filename),
  121. 'content' => $content,
  122. );
  123. return $info[$name];
  124. }
  125. }