file.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php namespace Laravel; use Closure;
  2. class File {
  3. /**
  4. * Determine if a file exists.
  5. *
  6. * @param string $path
  7. * @return bool
  8. */
  9. public static function exists($path)
  10. {
  11. return file_exists($path);
  12. }
  13. /**
  14. * Get the contents of a file.
  15. *
  16. * <code>
  17. * // Get the contents of a file
  18. * $contents = File::get(APP_PATH.'routes'.EXT);
  19. *
  20. * // Get the contents of a file or return a default value if it doesn't exist
  21. * $contents = File::get(APP_PATH.'routes'.EXT, 'Default Value');
  22. * </code>
  23. *
  24. * @param string $path
  25. * @param mixed $default
  26. * @return string
  27. */
  28. public static function get($path, $default = null)
  29. {
  30. if (file_exists($path))
  31. {
  32. return file_get_contents($path);
  33. }
  34. return ($default instanceof Closure) ? call_user_func($default) : $default;
  35. }
  36. /**
  37. * Write to a file.
  38. *
  39. * @param string $path
  40. * @param string $data
  41. * @return int
  42. */
  43. public static function put($path, $data)
  44. {
  45. return file_put_contents($path, $data, LOCK_EX);
  46. }
  47. /**
  48. * Append to a file.
  49. *
  50. * @param string $path
  51. * @param string $data
  52. * @return int
  53. */
  54. public static function append($path, $data)
  55. {
  56. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  57. }
  58. /**
  59. * Delete a file.
  60. *
  61. * @param string $path
  62. * @return void
  63. */
  64. public static function delete($path)
  65. {
  66. if (static::exists($path)) @unlink($path);
  67. }
  68. /**
  69. * Extract the file extension from a file path.
  70. *
  71. * @param string $path
  72. * @return string
  73. */
  74. public static function extension($path)
  75. {
  76. return pathinfo($path, PATHINFO_EXTENSION);
  77. }
  78. /**
  79. * Get the file type of a given file.
  80. *
  81. * @param string $path
  82. * @return string
  83. */
  84. public static function type($path)
  85. {
  86. return filetype($path);
  87. }
  88. /**
  89. * Get the file size of a given file.
  90. *
  91. * @param string $file
  92. * @return int
  93. */
  94. public static function size($path)
  95. {
  96. return filesize($path);
  97. }
  98. /**
  99. * Get the file's last modification time.
  100. *
  101. * @param string $path
  102. * @return int
  103. */
  104. public static function modified($path)
  105. {
  106. return filemtime($path);
  107. }
  108. /**
  109. * Move an uploaded file to permanent storage.
  110. *
  111. * <code>
  112. * // Upload the $_FILES['photo'] file to a permanent location
  113. * File::upload('photo', 'path/to/new/home.jpg');
  114. * </code>
  115. *
  116. * @param string $key
  117. * @param string $path
  118. * @return bool
  119. */
  120. public static function upload($key, $path)
  121. {
  122. if ( ! isset($_FILES[$key])) return false;
  123. return move_uploaded_file($_FILES[$key]['tmp_name'], $path);
  124. }
  125. /**
  126. * Get a file MIME type by extension.
  127. *
  128. * <code>
  129. * // Determine the MIME type for the .tar extension
  130. * $mime = File::mime('tar');
  131. *
  132. * // Return a default value if the MIME can't be determined
  133. * $mime = File::mime('ext', 'application/octet-stream');
  134. * </code>
  135. *
  136. * @param string $extension
  137. * @param string $default
  138. * @return string
  139. */
  140. public static function mime($extension, $default = 'application/octet-stream')
  141. {
  142. $mimes = Config::get('mimes');
  143. if ( ! array_key_exists($extension, $mimes)) return $default;
  144. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  145. }
  146. /**
  147. * Determine if a file is a given type.
  148. *
  149. * The Fileinfo PHP extension will be used to determine the MIME type of the file.
  150. *
  151. * <code>
  152. * // Determine if a file is a JPG image
  153. * $jpg = File::is('jpg', 'path/to/file.jpg');
  154. *
  155. * // Determine if a file is one of a given list of types
  156. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
  157. * </code>
  158. *
  159. * @param array|string $extension
  160. * @param string $path
  161. * @return bool
  162. */
  163. public static function is($extensions, $path)
  164. {
  165. $mimes = Config::get('mimes');
  166. foreach ((array) $extensions as $extension)
  167. {
  168. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  169. if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
  170. {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. }