file.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php namespace Laravel;
  2. /**
  3. * While this class may appear totally useless. It is actually quite helpful for dealing with
  4. * the global scope of the PHP file functions. Injecting this class into the classes that need
  5. * access to these functions allows us to test the classes without hitting the actual file system.
  6. */
  7. class File {
  8. /**
  9. * All of the MIME types understood by the manager.
  10. *
  11. * @var array
  12. */
  13. private $mimes;
  14. /**
  15. * Create a new file engine instance.
  16. *
  17. * @param array $mimes
  18. * @return void
  19. */
  20. public function __construct($mimes)
  21. {
  22. $this->mimes = $mimes;
  23. }
  24. /**
  25. * Determine if a file exists.
  26. *
  27. * @param string $path
  28. * @return bool
  29. */
  30. public function exists($path)
  31. {
  32. return file_exists($path);
  33. }
  34. /**
  35. * Get the contents of a file.
  36. *
  37. * @param string $path
  38. * @return string
  39. */
  40. public function get($path)
  41. {
  42. return file_get_contents($path);
  43. }
  44. /**
  45. * Write to a file.
  46. *
  47. * @param string $path
  48. * @param string $data
  49. * @return int
  50. */
  51. public function put($path, $data)
  52. {
  53. return file_put_contents($path, $data, LOCK_EX);
  54. }
  55. /**
  56. * Append to a file.
  57. *
  58. * @param string $path
  59. * @param string $data
  60. * @return int
  61. */
  62. public function append($path, $data)
  63. {
  64. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  65. }
  66. /**
  67. * Delete a file.
  68. *
  69. * @param string $path
  70. * @return void
  71. */
  72. public function delete($path)
  73. {
  74. @unlink($path);
  75. }
  76. /**
  77. * Extract the file extension from a file path.
  78. *
  79. * @param string $path
  80. * @return string
  81. */
  82. public function extension($path)
  83. {
  84. return pathinfo($path, PATHINFO_EXTENSION);
  85. }
  86. /**
  87. * Get the file type of a given file.
  88. *
  89. * @param string $path
  90. * @return string
  91. */
  92. public function type($path)
  93. {
  94. return filetype($path);
  95. }
  96. /**
  97. * Get the file size of a given file.
  98. *
  99. * @param string $file
  100. * @return int
  101. */
  102. public function size($path)
  103. {
  104. return filesize($path);
  105. }
  106. /**
  107. * Get the file's last modification time.
  108. *
  109. * @param string $path
  110. * @return int
  111. */
  112. public function modified($path)
  113. {
  114. return filemtime($path);
  115. }
  116. /**
  117. * Move an uploaded file to permanenet storage.
  118. *
  119. * @param string $key
  120. * @param string $path
  121. * @param array $files
  122. * @return bool
  123. */
  124. public function upload($key, $path, $files)
  125. {
  126. return move_uploaded_file($files[$key]['tmp_name'], $path);
  127. }
  128. /**
  129. * Get a file MIME type by extension.
  130. *
  131. * If the MIME type can't be determined, "application/octet-stream" will be returned.
  132. *
  133. * <code>
  134. * // Returns 'application/x-tar'
  135. * $mime = File::mime('path/to/file.tar');
  136. * </code>
  137. *
  138. * @param string $extension
  139. * @param string $default
  140. * @return string
  141. */
  142. public function mime($extension, $default = 'application/octet-stream')
  143. {
  144. if ( ! array_key_exists($extension, $this->mimes)) return $default;
  145. return (is_array($this->mimes[$extension])) ? $this->mimes[$extension][0] : $this->mimes[$extension];
  146. }
  147. /**
  148. * Determine if a file is a given type.
  149. *
  150. * The Fileinfo PHP extension will be used to determine the MIME type of the file.
  151. *
  152. * <code>
  153. * // Determine if a file is a JPG image
  154. * $image = File::is('jpg', 'path/to/image.jpg');
  155. *
  156. * // Determine if a file is any one of an array of types
  157. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/image.jpg');
  158. * </code>
  159. *
  160. * @param array|string $extension
  161. * @param string $path
  162. * @return bool
  163. */
  164. public function is($extensions, $path)
  165. {
  166. foreach ((array) $extensions as $extension)
  167. {
  168. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  169. if (isset($this->mimes[$extension]) and in_array((array) $this->mimes[$extension])) return true;
  170. }
  171. return false;
  172. }
  173. }