file.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 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 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 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 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 function delete($path)
  52. {
  53. @unlink($path);
  54. }
  55. /**
  56. * Extract the file extension from a file path.
  57. *
  58. * @param string $path
  59. * @return string
  60. */
  61. public 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 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 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 function modified($path)
  92. {
  93. return filemtime($path);
  94. }
  95. /**
  96. * Get the lines surrounding a given line in a file.
  97. *
  98. * The amount of padding with which to surround the line may also be specified.
  99. *
  100. * <code>
  101. * // Get lines 10 - 20 of the "routes.php" file
  102. * $lines = $file->snapshot(APP_PATH.'routes'.EXT, 15, 5);
  103. * </code>
  104. *
  105. * @param string $path
  106. * @param int $line
  107. * @param int $padding
  108. * @return array
  109. */
  110. public function snapshot($path, $line, $padding = 5)
  111. {
  112. if ( ! file_exists($path)) return array();
  113. $file = file($path, FILE_IGNORE_NEW_LINES);
  114. array_unshift($file, '');
  115. $start = $line - $padding;
  116. $length = ($line - $start) + $padding + 1;
  117. return array_slice($file, ($start > 0) ? $start : 0, ($length > 0) ? $length : 0, true);
  118. }
  119. /**
  120. * Get a file MIME type by extension.
  121. *
  122. * Any extension in the MIMEs configuration file may be passed to the method.
  123. *
  124. * <code>
  125. * // Returns "application/x-tar"
  126. * $mime = $file->mime('tar');
  127. * </code>
  128. *
  129. * @param string $extension
  130. * @param string $default
  131. * @return string
  132. */
  133. public function mime($extension, $default = 'application/octet-stream')
  134. {
  135. $mimes = Config::get('mimes');
  136. if ( ! array_key_exists($extension, $mimes)) return $default;
  137. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  138. }
  139. /**
  140. * Determine if a file is a given type.
  141. *
  142. * The Fileinfo PHP extension will be used to determine the MIME type of the file. Any extension
  143. * in the MIMEs configuration file may be passed as a type.
  144. *
  145. * <code>
  146. * // Determine if the file is a JPG image
  147. * $image = $file->is('jpg', 'path/to/image.jpg');
  148. * </code>
  149. *
  150. * @param string $extension
  151. * @param string $path
  152. * @return bool
  153. */
  154. public function is($extension, $path)
  155. {
  156. $mimes = Config::get('mimes');
  157. if ( ! array_key_exists($extension, $mimes))
  158. {
  159. throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
  160. }
  161. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  162. return (is_array($mimes[$extension])) ? in_array($mime, $mimes[$extension]) : $mime === $mimes[$extension];
  163. }
  164. }