您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Represents a file in the filesystem
  4. *
  5. * @warning Be sure to distinguish between get() and write() versus
  6. * read() and put(), the former operates on the entire file, while
  7. * the latter operates on a handle.
  8. */
  9. class FSTools_File
  10. {
  11. /** Filename of file this object represents */
  12. protected $name;
  13. /** Handle for the file */
  14. protected $handle = false;
  15. /** Instance of FSTools for interfacing with filesystem */
  16. protected $fs;
  17. /**
  18. * Filename of file you wish to instantiate.
  19. * @note This file need not exist
  20. */
  21. public function __construct($name, $fs = false)
  22. {
  23. $this->name = $name;
  24. $this->fs = $fs ? $fs : FSTools::singleton();
  25. }
  26. /** Returns the filename of the file. */
  27. public function getName() {return $this->name;}
  28. /** Returns directory of the file without trailing slash */
  29. public function getDirectory() {return $this->fs->dirname($this->name);}
  30. /**
  31. * Retrieves the contents of a file
  32. * @todo Throw an exception if file doesn't exist
  33. */
  34. public function get()
  35. {
  36. return $this->fs->file_get_contents($this->name);
  37. }
  38. /** Writes contents to a file, creates new file if necessary */
  39. public function write($contents)
  40. {
  41. return $this->fs->file_put_contents($this->name, $contents);
  42. }
  43. /** Deletes the file */
  44. public function delete()
  45. {
  46. return $this->fs->unlink($this->name);
  47. }
  48. /** Returns true if file exists and is a file. */
  49. public function exists()
  50. {
  51. return $this->fs->is_file($this->name);
  52. }
  53. /** Returns last file modification time */
  54. public function getMTime()
  55. {
  56. return $this->fs->filemtime($this->name);
  57. }
  58. /**
  59. * Chmod a file
  60. * @note We ignore errors because of some weird owner trickery due
  61. * to SVN duality
  62. */
  63. public function chmod($octal_code)
  64. {
  65. return @$this->fs->chmod($this->name, $octal_code);
  66. }
  67. /** Opens file's handle */
  68. public function open($mode)
  69. {
  70. if ($this->handle) $this->close();
  71. $this->handle = $this->fs->fopen($this->name, $mode);
  72. return true;
  73. }
  74. /** Closes file's handle */
  75. public function close()
  76. {
  77. if (!$this->handle) return false;
  78. $status = $this->fs->fclose($this->handle);
  79. $this->handle = false;
  80. return $status;
  81. }
  82. /** Retrieves a line from an open file, with optional max length $length */
  83. public function getLine($length = null)
  84. {
  85. if (!$this->handle) $this->open('r');
  86. if ($length === null) return $this->fs->fgets($this->handle);
  87. else return $this->fs->fgets($this->handle, $length);
  88. }
  89. /** Retrieves a character from an open file */
  90. public function getChar()
  91. {
  92. if (!$this->handle) $this->open('r');
  93. return $this->fs->fgetc($this->handle);
  94. }
  95. /** Retrieves an $length bytes of data from an open data */
  96. public function read($length)
  97. {
  98. if (!$this->handle) $this->open('r');
  99. return $this->fs->fread($this->handle, $length);
  100. }
  101. /** Writes to an open file */
  102. public function put($string)
  103. {
  104. if (!$this->handle) $this->open('a');
  105. return $this->fs->fwrite($this->handle, $string);
  106. }
  107. /** Returns TRUE if the end of the file has been reached */
  108. public function eof()
  109. {
  110. if (!$this->handle) return true;
  111. return $this->fs->feof($this->handle);
  112. }
  113. public function __destruct()
  114. {
  115. if ($this->handle) $this->close();
  116. }
  117. }
  118. // vim: et sw=4 sts=4