Packages

  • 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
  • Package
  • 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:  * @package Nette\Application\Responses
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * File download response.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read string $file
 21:  * @property-read string $name
 22:  * @property-read string $contentType
 23:  * @package Nette\Application\Responses
 24:  */
 25: class FileResponse extends Object implements IPresenterResponse
 26: {
 27:     /** @var string */
 28:     private $file;
 29: 
 30:     /** @var string */
 31:     private $contentType;
 32: 
 33:     /** @var string */
 34:     private $name;
 35: 
 36:     /** @var bool */
 37:     public $resuming = TRUE;
 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)
 46:     {
 47:         if (!is_file($file)) {
 48:             throw new 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:     }
 55: 
 56: 
 57: 
 58:     /**
 59:      * Returns the path to a downloaded file.
 60:      * @return string
 61:      */
 62:     final public function getFile()
 63:     {
 64:         return $this->file;
 65:     }
 66: 
 67: 
 68: 
 69:     /**
 70:      * Returns the file name.
 71:      * @return string
 72:      */
 73:     final public function getName()
 74:     {
 75:         return $this->name;
 76:     }
 77: 
 78: 
 79: 
 80:     /**
 81:      * Returns the MIME content type of a downloaded file.
 82:      * @return string
 83:      */
 84:     final public function getContentType()
 85:     {
 86:         return $this->contentType;
 87:     }
 88: 
 89: 
 90: 
 91:     /**
 92:      * Sends response to output.
 93:      * @return void
 94:      */
 95:     public function send(IHttpRequest $httpRequest, IHttpResponse $httpResponse)
 96:     {
 97:         $httpResponse->setContentType($this->contentType);
 98:         $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
 99: 
100:         $filesize = $length = filesize($this->file);
101:         $handle = fopen($this->file, 'r');
102: 
103:         if ($this->resuming) {
104:             $httpResponse->setHeader('Accept-Ranges', 'bytes');
105:             $range = $httpRequest->getHeader('Range');
106:             if ($range !== NULL) {
107:                 $range = substr($range, 6); // 6 == strlen('bytes=')
108:                 list($start, $end) = explode('-', $range);
109:                 if ($start == NULL) {
110:                     $start = 0;
111:                 }
112:                 if ($end == NULL) {
113:                     $end = $filesize - 1;
114:                 }
115: 
116:                 if ($start < 0 || $end <= $start || $end > $filesize -1) {
117:                     $httpResponse->setCode(416); // requested range not satisfiable
118:                     return;
119:                 }
120: 
121:                 $httpResponse->setCode(206);
122:                 $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
123:                 $length = $end - $start + 1;
124:                 fseek($handle, $start);
125: 
126:             } else {
127:                 $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
128:             }
129:         }
130: 
131:         $httpResponse->setHeader('Content-Length', $length);
132:         while (!feof($handle)) {
133:             echo fread($handle, 4e6);
134:         }
135:         fclose($handle);
136:     }
137: 
138: }
139: 
Nette Framework 2.0.1 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0