Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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