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.
 
 
 
 

97 lines
2.3 KiB

  1. <?php
  2. namespace Kuxin\Storage;
  3. class File
  4. {
  5. protected $path = null;
  6. protected $url = null;
  7. public function __construct($config)
  8. {
  9. if (strpos($config['path'], "ROOT") !== false) {
  10. $config['path'] = str_replace('ROOT', KX_ROOT, $config['path']);
  11. }
  12. $this->path = $config['path'];
  13. $this->url = $config['url'] ?? "";
  14. }
  15. public function exist($file)
  16. {
  17. return is_file($this->getPath($file));
  18. }
  19. public function mtime($file)
  20. {
  21. return filemtime($this->getPath($file));
  22. }
  23. public function write($file, $content)
  24. {
  25. $fullfile = $this->getPath($file);
  26. if (!is_dir(dirname($fullfile))) {
  27. mkdir(dirname($fullfile), 0755, true);
  28. }
  29. return file_put_contents($fullfile, (string)$content);
  30. }
  31. public function read($file)
  32. {
  33. $fullfile = $this->getPath($file);
  34. if (is_file($fullfile)) {
  35. return file_get_contents($fullfile);
  36. } else {
  37. return false;
  38. }
  39. }
  40. public function append($file, $content)
  41. {
  42. $fullfile = $this->getPath($file);
  43. if (!is_dir(dirname($fullfile))) {
  44. mkdir(dirname($fullfile), 0755, true);
  45. }
  46. return file_put_contents($fullfile, (string)$content, FILE_APPEND);
  47. }
  48. public function remove($file)
  49. {
  50. $file = $this->getPath($file);
  51. if (is_file($file)) {
  52. //删除文件
  53. return unlink($file);
  54. } elseif (is_dir($file)) {
  55. //删除目录
  56. $handle = opendir($file);
  57. while (($filename = readdir($handle)) !== false) {
  58. if ($filename !== '.' && $filename !== '..') {
  59. $this->remove($file . '/' . $filename);
  60. }
  61. }
  62. closedir($handle);
  63. return rmdir($file);
  64. }
  65. }
  66. public function getUrl($file)
  67. {
  68. $file = ($file{0} === '/' || $file{1} == ':') ? str_replace($this->path, '', $file) : $file;
  69. return rtrim($this->url, '/') . '/' . $file;
  70. }
  71. public function getPath($file)
  72. {
  73. return ($file{0} === '/' || $file{1} == ':') ? $file : $this->path . '/' . ltrim($file, '/');
  74. }
  75. public function error()
  76. {
  77. return '';
  78. }
  79. public function flush()
  80. {
  81. return $this->remove('');
  82. }
  83. }