download.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php namespace Laravel;
  2. class Download extends Response {
  3. /**
  4. * Create a new download response instance.
  5. *
  6. * <code>
  7. * // Return a download response for a given file
  8. * return new Download('path/to/image.jpg');
  9. *
  10. * // Return a download response for a given file and assign a name
  11. * return new Download('path/to/image.jpg', 'you.jpg');
  12. * </code>
  13. *
  14. * @param string $path
  15. * @param string $name
  16. */
  17. public function __construct($path, $name = null)
  18. {
  19. if (is_null($name)) $name = basename($path);
  20. $file = IoC::container()->resolve('laravel.file');
  21. parent::__construct($file->get($path));
  22. $this->header('Content-Description', 'File Transfer');
  23. $this->header('Content-Type', $file->mime($file->extension($path)));
  24. $this->header('Content-Disposition', 'attachment; filename="'.$name.'"');
  25. $this->header('Content-Transfer-Encoding', 'binary');
  26. $this->header('Expires', 0);
  27. $this->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  28. $this->header('Pragma', 'public');
  29. $this->header('Content-Length', $file->size($path));
  30. }
  31. }