file.php 3.5 KB

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