file.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php namespace Laravel; use Closure, FilesystemIterator as fIterator;
  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(path('app').'routes'.EXT);
  19. *
  20. * // Get the contents of a file or return a default value if it doesn't exist
  21. * $contents = File::get(path('app').'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 bool
  59. */
  60. public static function delete($path)
  61. {
  62. if (static::exists($path)) return @unlink($path);
  63. }
  64. /**
  65. * Move a file to a new location.
  66. *
  67. * @param string $path
  68. * @param string $target
  69. * @return void
  70. */
  71. public static function move($path, $target)
  72. {
  73. return rename($path, $target);
  74. }
  75. /**
  76. * Copy a file to a new location.
  77. *
  78. * @param string $path
  79. * @param string $target
  80. * @return void
  81. */
  82. public static function copy($path, $target)
  83. {
  84. return copy($path, $target);
  85. }
  86. /**
  87. * Extract the file extension from a file path.
  88. *
  89. * @param string $path
  90. * @return string
  91. */
  92. public static function extension($path)
  93. {
  94. return pathinfo($path, PATHINFO_EXTENSION);
  95. }
  96. /**
  97. * Get the file type of a given file.
  98. *
  99. * @param string $path
  100. * @return string
  101. */
  102. public static function type($path)
  103. {
  104. return filetype($path);
  105. }
  106. /**
  107. * Get the file size of a given file.
  108. *
  109. * @param string $path
  110. * @return int
  111. */
  112. public static function size($path)
  113. {
  114. return filesize($path);
  115. }
  116. /**
  117. * Get the file's last modification time.
  118. *
  119. * @param string $path
  120. * @return int
  121. */
  122. public static function modified($path)
  123. {
  124. return filemtime($path);
  125. }
  126. /**
  127. * Get a file MIME type by extension.
  128. *
  129. * <code>
  130. * // Determine the MIME type for the .tar extension
  131. * $mime = File::mime('tar');
  132. *
  133. * // Return a default value if the MIME can't be determined
  134. * $mime = File::mime('ext', 'application/octet-stream');
  135. * </code>
  136. *
  137. * @param string $extension
  138. * @param string $default
  139. * @return string
  140. */
  141. public static function mime($extension, $default = 'application/octet-stream')
  142. {
  143. $mimes = Config::get('mimes');
  144. if ( ! array_key_exists($extension, $mimes)) return $default;
  145. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  146. }
  147. /**
  148. * Determine if a file is a given type.
  149. *
  150. * The Fileinfo PHP extension is used to determine the file's MIME type.
  151. *
  152. * <code>
  153. * // Determine if a file is a JPG image
  154. * $jpg = File::is('jpg', 'path/to/file.jpg');
  155. *
  156. * // Determine if a file is one of a given list of types
  157. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
  158. * </code>
  159. *
  160. * @param array|string $extensions
  161. * @param string $path
  162. * @return bool
  163. */
  164. public static function is($extensions, $path)
  165. {
  166. $mimes = Config::get('mimes');
  167. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  168. // The MIME configuration file contains an array of file extensions and
  169. // their associated MIME types. We will spin through each extension the
  170. // developer wants to check and look for the MIME type.
  171. foreach ((array) $extensions as $extension)
  172. {
  173. if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
  174. {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * Create a new directory.
  182. *
  183. * @param string $path
  184. * @param int $chmod
  185. * @return void
  186. */
  187. public static function mkdir($path, $chmod = 0777)
  188. {
  189. return ( ! is_dir($path)) ? mkdir($path, $chmod, true) : true;
  190. }
  191. /**
  192. * Move a directory from one location to another.
  193. *
  194. * @param string $source
  195. * @param string $destination
  196. * @param int $options
  197. * @return void
  198. */
  199. public static function mvdir($source, $destination, $options = fIterator::SKIP_DOTS)
  200. {
  201. return static::cpdir($source, $destination, true, $options);
  202. }
  203. /**
  204. * Recursively copy directory contents to another directory.
  205. *
  206. * @param string $source
  207. * @param string $destination
  208. * @param bool $delete
  209. * @param int $options
  210. * @return void
  211. */
  212. public static function cpdir($source, $destination, $delete = false, $options = fIterator::SKIP_DOTS)
  213. {
  214. if ( ! is_dir($source)) return false;
  215. // First we need to create the destination directory if it doesn't
  216. // already exists. This directory hosts all of the assets we copy
  217. // from the installed bundle's source directory.
  218. if ( ! is_dir($destination))
  219. {
  220. mkdir($destination, 0777, true);
  221. }
  222. $items = new fIterator($source, $options);
  223. foreach ($items as $item)
  224. {
  225. $location = $destination.DS.$item->getBasename();
  226. // If the file system item is a directory, we will recurse the
  227. // function, passing in the item directory. To get the proper
  228. // destination path, we'll add the basename of the source to
  229. // to the destination directory.
  230. if ($item->isDir())
  231. {
  232. $path = $item->getRealPath();
  233. if (! static::cpdir($path, $location, $delete, $options)) return false;
  234. if ($delete) @rmdir($item->getRealPath());
  235. }
  236. // If the file system item is an actual file, we can copy the
  237. // file from the bundle asset directory to the public asset
  238. // directory. The "copy" method will overwrite any existing
  239. // files with the same name.
  240. else
  241. {
  242. if(! copy($item->getRealPath(), $location)) return false;
  243. if ($delete) @unlink($item->getRealPath());
  244. }
  245. }
  246. unset($items);
  247. if ($delete) @rmdir($source);
  248. return true;
  249. }
  250. /**
  251. * Recursively delete a directory.
  252. *
  253. * @param string $directory
  254. * @param bool $preserve
  255. * @return void
  256. */
  257. public static function rmdir($directory, $preserve = false)
  258. {
  259. if ( ! is_dir($directory)) return;
  260. $items = new fIterator($directory);
  261. foreach ($items as $item)
  262. {
  263. // If the item is a directory, we can just recurse into the
  264. // function and delete that sub-directory, otherwise we'll
  265. // just deleete the file and keep going!
  266. if ($item->isDir())
  267. {
  268. static::rmdir($item->getRealPath());
  269. }
  270. else
  271. {
  272. @unlink($item->getRealPath());
  273. }
  274. }
  275. unset($items);
  276. if ( ! $preserve) @rmdir($directory);
  277. }
  278. /**
  279. * Empty the specified directory of all files and folders.
  280. *
  281. * @param string $directory
  282. * @return void
  283. */
  284. public static function cleandir($directory)
  285. {
  286. return static::rmdir($directory, true);
  287. }
  288. /**
  289. * Get the most recently modified file in a directory.
  290. *
  291. * @param string $directory
  292. * @param int $options
  293. * @return SplFileInfo
  294. */
  295. public static function latest($directory, $options = fIterator::SKIP_DOTS)
  296. {
  297. $time = 0;
  298. $items = new fIterator($directory, $options);
  299. // To get the latest created file, we'll simply spin through the
  300. // directory, setting the latest file if we encounter a file
  301. // with a UNIX timestamp greater than the latest one.
  302. foreach ($items as $item)
  303. {
  304. if ($item->getMTime() > $time) $latest = $item;
  305. }
  306. return $latest;
  307. }
  308. }