File.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Swift Mailer File Stream Wrapper
  4. * Please read the LICENSE file
  5. * @copyright Chris Corbyn <chris@w3style.co.uk>
  6. * @author Chris Corbyn <chris@w3style.co.uk>
  7. * @package Swift
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/ClassLoader.php";
  11. Swift_ClassLoader::load("Swift_FileException");
  12. /**
  13. * Swift File stream abstraction layer
  14. * Reads bytes from a file
  15. * @package Swift
  16. * @author Chris Corbyn <chris@w3style.co.uk>
  17. */
  18. class Swift_File
  19. {
  20. /**
  21. * The accessible path to the file
  22. * @var string
  23. */
  24. protected $path = null;
  25. /**
  26. * The name of the file
  27. * @var string
  28. */
  29. protected $name = null;
  30. /**
  31. * The resource returned by fopen() against the path
  32. * @var resource
  33. */
  34. protected $handle = null;
  35. /**
  36. * The status of magic_quotes in php.ini
  37. * @var boolean
  38. */
  39. protected $magic_quotes = false;
  40. /**
  41. * Constructor
  42. * @param string The path the the file
  43. * @throws Swift_FileException If the file cannot be found
  44. */
  45. public function __construct($path)
  46. {
  47. $this->setPath($path);
  48. $this->magic_quotes = get_magic_quotes_runtime();
  49. }
  50. /**
  51. * Set the path to the file
  52. * @param string The path to the file
  53. * @throws Swift_FileException If the file cannot be found
  54. */
  55. public function setPath($path)
  56. {
  57. if (!file_exists($path))
  58. {
  59. throw new Swift_FileException("No such file '" . $path ."'");
  60. }
  61. $this->handle = null;
  62. $this->path = $path;
  63. $this->name = null;
  64. $this->name = $this->getFileName();
  65. }
  66. /**
  67. * Get the path to the file
  68. * @return string
  69. */
  70. public function getPath()
  71. {
  72. return $this->path;
  73. }
  74. /**
  75. * Get the name of the file without it's full path
  76. * @return string
  77. */
  78. public function getFileName()
  79. {
  80. if ($this->name !== null)
  81. {
  82. return $this->name;
  83. }
  84. else
  85. {
  86. return basename($this->getPath());
  87. }
  88. }
  89. /**
  90. * Establish an open file handle on the file if the file is not yet opened
  91. * @throws Swift_FileException If the file cannot be opened for reading
  92. */
  93. protected function createHandle()
  94. {
  95. if ($this->handle === null)
  96. {
  97. if (!$this->handle = fopen($this->path, "rb"))
  98. {
  99. throw new Swift_FileException("Unable to open file '" . $this->path . " for reading. Check the file permissions.");
  100. }
  101. }
  102. }
  103. /**
  104. * Check if the pointer as at the end of the file
  105. * @return boolean
  106. * @throws Swift_FileException If the file cannot be read
  107. */
  108. public function EOF()
  109. {
  110. $this->createHandle();
  111. return feof($this->handle);
  112. }
  113. /**
  114. * Get a single byte from the file
  115. * Returns false past EOF
  116. * @return string
  117. * @throws Swift_FileException If the file cannot be read
  118. */
  119. public function getByte()
  120. {
  121. $this->createHandle();
  122. return $this->read(1);
  123. }
  124. /**
  125. * Read one full line from the file including the line ending
  126. * Returns false past EOF
  127. * @return string
  128. * @throws Swift_FileException If the file cannot be read
  129. */
  130. public function readln()
  131. {
  132. set_magic_quotes_runtime(0);
  133. $this->createHandle();
  134. if (!$this->EOF())
  135. {
  136. $ret = fgets($this->handle);
  137. }
  138. else $ret = false;
  139. set_magic_quotes_runtime($this->magic_quotes);
  140. return $ret;
  141. }
  142. /**
  143. * Get the entire file contents as a string
  144. * @return string
  145. * @throws Swift_FileException If the file cannot be read
  146. */
  147. public function readFull()
  148. {
  149. $ret = "";
  150. set_magic_quotes_runtime(0);
  151. while (false !== $chunk = $this->read(8192, false)) $ret .= $chunk;
  152. set_magic_quotes_runtime($this->magic_quotes);
  153. return $ret;
  154. }
  155. /**
  156. * Read a given number of bytes from the file
  157. * Returns false past EOF
  158. * @return string
  159. * @throws Swift_FileException If the file cannot be read
  160. */
  161. public function read($bytes, $unquote=true)
  162. {
  163. if ($unquote) set_magic_quotes_runtime(0);
  164. $this->createHandle();
  165. if (!$this->EOF())
  166. {
  167. $ret = fread($this->handle, $bytes);
  168. }
  169. else $ret = false;
  170. if ($unquote) set_magic_quotes_runtime($this->magic_quotes);
  171. return $ret;
  172. }
  173. /**
  174. * Get the size of the file in bytes
  175. * @return int
  176. */
  177. public function length()
  178. {
  179. return filesize($this->path);
  180. }
  181. /**
  182. * Close the open handle on the file
  183. * @throws Swift_FileException If the file cannot be read
  184. */
  185. public function close()
  186. {
  187. $this->createHandle();
  188. fclose($this->handle);
  189. $this->handle = null;
  190. }
  191. /**
  192. * Reset the file pointer back to zero
  193. */
  194. public function reset()
  195. {
  196. $this->createHandle();
  197. fseek($this->handle, 0);
  198. }
  199. /**
  200. * Destructor
  201. * Closes the file
  202. */
  203. public function __destruct()
  204. {
  205. if ($this->handle !== null) $this->close();
  206. }
  207. }