File.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Intervention\Image;
  3. class File
  4. {
  5. /**
  6. * Mime type
  7. *
  8. * @var string
  9. */
  10. public $mime;
  11. /**
  12. * Name of directory path
  13. *
  14. * @var string
  15. */
  16. public $dirname;
  17. /**
  18. * Basename of current file
  19. *
  20. * @var string
  21. */
  22. public $basename;
  23. /**
  24. * File extension of current file
  25. *
  26. * @var string
  27. */
  28. public $extension;
  29. /**
  30. * File name of current file
  31. *
  32. * @var string
  33. */
  34. public $filename;
  35. /**
  36. * Sets all instance properties from given path
  37. *
  38. * @param string $path
  39. */
  40. public function setFileInfoFromPath($path)
  41. {
  42. $info = pathinfo($path);
  43. $this->dirname = array_key_exists('dirname', $info) ? $info['dirname'] : null;
  44. $this->basename = array_key_exists('basename', $info) ? $info['basename'] : null;
  45. $this->extension = array_key_exists('extension', $info) ? $info['extension'] : null;
  46. $this->filename = array_key_exists('filename', $info) ? $info['filename'] : null;
  47. if (file_exists($path) && is_file($path)) {
  48. $this->mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Get file size
  54. *
  55. * @return mixed
  56. */
  57. public function filesize()
  58. {
  59. $path = $this->basePath();
  60. if (file_exists($path) && is_file($path)) {
  61. return filesize($path);
  62. }
  63. return false;
  64. }
  65. }