file.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php namespace Laravel;
  2. class File {
  3. /**
  4. * All of the MIME types understood by the manager.
  5. *
  6. * @var array
  7. */
  8. private $mimes;
  9. /**
  10. * Create a new file manager instance.
  11. *
  12. * @param array $mimes
  13. * @return void
  14. */
  15. public function __construct($mimes)
  16. {
  17. $this->mimes = $mimes;
  18. }
  19. /**
  20. * Determine if a file exists.
  21. *
  22. * @param string $path
  23. * @return bool
  24. */
  25. public function exists($path)
  26. {
  27. return file_exists($path);
  28. }
  29. /**
  30. * Get the contents of a file.
  31. *
  32. * @param string $path
  33. * @return string
  34. */
  35. public function get($path)
  36. {
  37. return file_get_contents($path);
  38. }
  39. /**
  40. * Write to a file.
  41. *
  42. * @param string $path
  43. * @param string $data
  44. * @return int
  45. */
  46. public function put($path, $data)
  47. {
  48. return file_put_contents($path, $data, LOCK_EX);
  49. }
  50. /**
  51. * Append to a file.
  52. *
  53. * @param string $path
  54. * @param string $data
  55. * @return int
  56. */
  57. public function append($path, $data)
  58. {
  59. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  60. }
  61. /**
  62. * Delete a file.
  63. *
  64. * @param string $path
  65. * @return void
  66. */
  67. public function delete($path)
  68. {
  69. @unlink($path);
  70. }
  71. /**
  72. * Extract the file extension from a file path.
  73. *
  74. * @param string $path
  75. * @return string
  76. */
  77. public function extension($path)
  78. {
  79. return pathinfo($path, PATHINFO_EXTENSION);
  80. }
  81. /**
  82. * Get the file type of a given file.
  83. *
  84. * @param string $path
  85. * @return string
  86. */
  87. public function type($path)
  88. {
  89. return filetype($path);
  90. }
  91. /**
  92. * Get the file size of a given file.
  93. *
  94. * @param string $file
  95. * @return int
  96. */
  97. public function size($path)
  98. {
  99. return filesize($path);
  100. }
  101. /**
  102. * Get the file's last modification time.
  103. *
  104. * @param string $path
  105. * @return int
  106. */
  107. public function modified($path)
  108. {
  109. return filemtime($path);
  110. }
  111. /**
  112. * Get a file MIME type by extension.
  113. *
  114. * @param string $extension
  115. * @param string $default
  116. * @return string
  117. */
  118. public function mime($extension, $default = 'application/octet-stream')
  119. {
  120. if ( ! array_key_exists($extension, $this->mimes)) return $default;
  121. return (is_array($this->mimes[$extension])) ? $this->mimes[$extension][0] : $this->mimes[$extension];
  122. }
  123. /**
  124. * Determine if a file is a given type.
  125. *
  126. * The Fileinfo PHP extension will be used to determine the MIME type of the file.
  127. *
  128. * @param string $extension
  129. * @param string $path
  130. * @return bool
  131. */
  132. public function is($extension, $path)
  133. {
  134. if ( ! array_key_exists($extension, $this->mimes))
  135. {
  136. throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
  137. }
  138. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  139. return (is_array($this->mimes[$extension])) ? in_array($mime, $this->mimes[$extension]) : $mime === $this->mimes[$extension];
  140. }
  141. }