file.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php namespace Laravel; use Closure, FilesystemIterator;
  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($GLOBALS['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($GLOBALS['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. return (file_exists($path)) ? file_get_contents($path) : value($default);
  31. }
  32. /**
  33. * Write to a file.
  34. *
  35. * @param string $path
  36. * @param string $data
  37. * @return int
  38. */
  39. public static function put($path, $data)
  40. {
  41. return file_put_contents($path, $data, LOCK_EX);
  42. }
  43. /**
  44. * Append to a file.
  45. *
  46. * @param string $path
  47. * @param string $data
  48. * @return int
  49. */
  50. public static function append($path, $data)
  51. {
  52. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  53. }
  54. /**
  55. * Delete a file.
  56. *
  57. * @param string $path
  58. * @return void
  59. */
  60. public static function delete($path)
  61. {
  62. if (static::exists($path)) @unlink($path);
  63. }
  64. /**
  65. * Extract the file extension from a file path.
  66. *
  67. * @param string $path
  68. * @return string
  69. */
  70. public static function extension($path)
  71. {
  72. return pathinfo($path, PATHINFO_EXTENSION);
  73. }
  74. /**
  75. * Get the file type of a given file.
  76. *
  77. * @param string $path
  78. * @return string
  79. */
  80. public static function type($path)
  81. {
  82. return filetype($path);
  83. }
  84. /**
  85. * Get the file size of a given file.
  86. *
  87. * @param string $file
  88. * @return int
  89. */
  90. public static function size($path)
  91. {
  92. return filesize($path);
  93. }
  94. /**
  95. * Get the file's last modification time.
  96. *
  97. * @param string $path
  98. * @return int
  99. */
  100. public static function modified($path)
  101. {
  102. return filemtime($path);
  103. }
  104. /**
  105. * Get a file MIME type by extension.
  106. *
  107. * <code>
  108. * // Determine the MIME type for the .tar extension
  109. * $mime = File::mime('tar');
  110. *
  111. * // Return a default value if the MIME can't be determined
  112. * $mime = File::mime('ext', 'application/octet-stream');
  113. * </code>
  114. *
  115. * @param string $extension
  116. * @param string $default
  117. * @return string
  118. */
  119. public static function mime($extension, $default = 'application/octet-stream')
  120. {
  121. $mimes = Config::get('mimes');
  122. if ( ! array_key_exists($extension, $mimes)) return $default;
  123. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  124. }
  125. /**
  126. * Determine if a file is a given type.
  127. *
  128. * The Fileinfo PHP extension is used to determine the file's MIME type.
  129. *
  130. * <code>
  131. * // Determine if a file is a JPG image
  132. * $jpg = File::is('jpg', 'path/to/file.jpg');
  133. *
  134. * // Determine if a file is one of a given list of types
  135. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
  136. * </code>
  137. *
  138. * @param array|string $extension
  139. * @param string $path
  140. * @return bool
  141. */
  142. public static function is($extensions, $path)
  143. {
  144. $mimes = Config::get('mimes');
  145. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  146. // The MIME configuration file contains an array of file extensions and
  147. // their associated MIME types. We will spin through each extension the
  148. // developer wants to check to determine if the file's MIME type is in
  149. // the list of MIMEs we have for that extension.
  150. foreach ((array) $extensions as $extension)
  151. {
  152. if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
  153. {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. /**
  160. * Recursively copy directory contents to another directory.
  161. *
  162. * @param string $source
  163. * @param string $destination
  164. * @return void
  165. */
  166. public static function cpdir($source, $destination)
  167. {
  168. if ( ! is_dir($source)) return;
  169. // First we need to create the destination directory if it doesn't
  170. // already exists. This directory hosts all of the assets we copy
  171. // from the installed bundle's source directory.
  172. if ( ! is_dir($destination))
  173. {
  174. mkdir($destination);
  175. }
  176. $items = new FilesystemIterator($source, FilesystemIterator::SKIP_DOTS);
  177. foreach ($items as $item)
  178. {
  179. $location = $destination.DS.$item->getBasename();
  180. // If the file system item is a directory, we will recurse the
  181. // function, passing in the item directory. To get the proper
  182. // destination path, we'll add the basename of the source to
  183. // to the destination directory.
  184. if ($item->isDir())
  185. {
  186. $path = $item->getRealPath();
  187. static::cpdir($path, $location);
  188. }
  189. // If the file system item is an actual file, we can copy the
  190. // file from the bundle asset directory to the public asset
  191. // directory. The "copy" method will overwrite any existing
  192. // files with the same name.
  193. else
  194. {
  195. copy($item->getRealPath(), $location);
  196. }
  197. }
  198. }
  199. }