file.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @param string $key
  99. * @param string $path
  100. * @param array $files
  101. * @return bool
  102. */
  103. public static function upload($key, $path, $files = null)
  104. {
  105. if (is_null($files)) $files = $_FILES;
  106. return move_uploaded_file($files[$key]['tmp_name'], $path);
  107. }
  108. /**
  109. * Get a file MIME type by extension.
  110. *
  111. * <code>
  112. * // Determine the MIME type for the .tar extension
  113. * $mime = File::mime('tar');
  114. *
  115. * // Return a default value if the MIME can't be determined
  116. * $mime = File::mime('ext', 'application/octet-stream');
  117. * </code>
  118. *
  119. * @param string $extension
  120. * @param string $default
  121. * @return string
  122. */
  123. public static function mime($extension, $default = 'application/octet-stream')
  124. {
  125. $mimes = Config::get('mimes');
  126. if ( ! array_key_exists($extension, $mimes)) return $default;
  127. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  128. }
  129. /**
  130. * Determine if a file is a given type.
  131. *
  132. * The Fileinfo PHP extension will be used to determine the MIME type of the file.
  133. *
  134. * <code>
  135. * // Determine if a file is a JPG image
  136. * $jpg = File::is('jpg', 'path/to/file.jpg');
  137. *
  138. * // Determine if a file is one of a given list of types
  139. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
  140. * </code>
  141. *
  142. * @param array|string $extension
  143. * @param string $path
  144. * @return bool
  145. */
  146. public static function is($extensions, $path)
  147. {
  148. $mimes = Config::get('mimes');
  149. foreach ((array) $extensions as $extension)
  150. {
  151. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  152. if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
  153. {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. }