FileHelpers.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Illuminate\Http;
  3. use Illuminate\Support\Str;
  4. trait FileHelpers
  5. {
  6. /**
  7. * The cache copy of the file's hash name.
  8. *
  9. * @var string
  10. */
  11. protected $hashName = null;
  12. /**
  13. * Get the fully qualified path to the file.
  14. *
  15. * @return string
  16. */
  17. public function path()
  18. {
  19. return $this->getRealPath();
  20. }
  21. /**
  22. * Get the file's extension.
  23. *
  24. * @return string
  25. */
  26. public function extension()
  27. {
  28. return $this->guessExtension();
  29. }
  30. /**
  31. * Get the file's extension supplied by the client.
  32. *
  33. * @return string
  34. */
  35. public function clientExtension()
  36. {
  37. return $this->guessClientExtension();
  38. }
  39. /**
  40. * Get a filename for the file.
  41. *
  42. * @param string $path
  43. * @return string
  44. */
  45. public function hashName($path = null)
  46. {
  47. if ($path) {
  48. $path = rtrim($path, '/').'/';
  49. }
  50. $hash = $this->hashName ?: $this->hashName = Str::random(40);
  51. return $path.$hash.'.'.$this->guessExtension();
  52. }
  53. }