Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • FileResponse
  • ForwardResponse
  • JsonResponse
  • RedirectResponse
  • TextResponse
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Application\Responses;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * File download response.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property-read string $file
 19:  * @property-read string $name
 20:  * @property-read string $contentType
 21:  */
 22: class FileResponse extends Nette\Object implements Nette\Application\IResponse
 23: {
 24:     /** @var string */
 25:     private $file;
 26: 
 27:     /** @var string */
 28:     private $contentType;
 29: 
 30:     /** @var string */
 31:     private $name;
 32: 
 33:     /** @var bool */
 34:     public $resuming = TRUE;
 35: 
 36:     /** @var bool */
 37:     private $forceDownload;
 38: 
 39: 
 40:     /**
 41:      * @param  string  file path
 42:      * @param  string  imposed file name
 43:      * @param  string  MIME content type
 44:      */
 45:     public function __construct($file, $name = NULL, $contentType = NULL, $forceDownload = TRUE)
 46:     {
 47:         if (!is_file($file)) {
 48:             throw new Nette\Application\BadRequestException("File '$file' doesn't exist.");
 49:         }
 50: 
 51:         $this->file = $file;
 52:         $this->name = $name ? $name : basename($file);
 53:         $this->contentType = $contentType ? $contentType : 'application/octet-stream';
 54:         $this->forceDownload = $forceDownload;
 55:     }
 56: 
 57: 
 58:     /**
 59:      * Returns the path to a downloaded file.
 60:      * @return string
 61:      */
 62:     public function getFile()
 63:     {
 64:         return $this->file;
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Returns the file name.
 70:      * @return string
 71:      */
 72:     public function getName()
 73:     {
 74:         return $this->name;
 75:     }
 76: 
 77: 
 78:     /**
 79:      * Returns the MIME content type of a downloaded file.
 80:      * @return string
 81:      */
 82:     public function getContentType()
 83:     {
 84:         return $this->contentType;
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Sends response to output.
 90:      * @return void
 91:      */
 92:     public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 93:     {
 94:         $httpResponse->setContentType($this->contentType);
 95:         $httpResponse->setHeader('Content-Disposition',
 96:             ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $this->name . '"');
 97: 
 98:         $filesize = $length = filesize($this->file);
 99:         $handle = fopen($this->file, 'r');
100: 
101:         if ($this->resuming) {
102:             $httpResponse->setHeader('Accept-Ranges', 'bytes');
103:             if (preg_match('#^bytes=(\d*)-(\d*)\z#', $httpRequest->getHeader('Range'), $matches)) {
104:                 list(, $start, $end) = $matches;
105:                 if ($start === '') {
106:                     $start = max(0, $filesize - $end);
107:                     $end = $filesize - 1;
108: 
109:                 } elseif ($end === '' || $end > $filesize - 1) {
110:                     $end = $filesize - 1;
111:                 }
112:                 if ($end < $start) {
113:                     $httpResponse->setCode(416); // requested range not satisfiable
114:                     return;
115:                 }
116: 
117:                 $httpResponse->setCode(206);
118:                 $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
119:                 $length = $end - $start + 1;
120:                 fseek($handle, $start);
121: 
122:             } else {
123:                 $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
124:             }
125:         }
126: 
127:         $httpResponse->setHeader('Content-Length', $length);
128:         while (!feof($handle) && $length > 0) {
129:             echo $s = fread($handle, min(4e6, $length));
130:             $length -= strlen($s);
131:         }
132:         fclose($handle);
133:     }
134: 
135: }
136: 
Nette 2.2.6 API API documentation generated by ApiGen 2.8.0