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

  • NFileResponse
  • NForwardResponse
  • NJsonResponse
  • NRedirectResponse
  • NTextResponse
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  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 NFileResponse extends NObject 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 NBadRequestException("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:      * Returns the path to a downloaded file.
 59:      * @return string
 60:      */
 61:     final public function getFile()
 62:     {
 63:         return $this->file;
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Returns the file name.
 69:      * @return string
 70:      */
 71:     final public function getName()
 72:     {
 73:         return $this->name;
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Returns the MIME content type of a downloaded file.
 79:      * @return string
 80:      */
 81:     final public function getContentType()
 82:     {
 83:         return $this->contentType;
 84:     }
 85: 
 86: 
 87:     /**
 88:      * Sends response to output.
 89:      * @return void
 90:      */
 91:     public function send(IHttpRequest $httpRequest, IHttpResponse $httpResponse)
 92:     {
 93:         $httpResponse->setContentType($this->contentType);
 94:         $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
 95: 
 96:         $filesize = $length = filesize($this->file);
 97:         $handle = fopen($this->file, 'r');
 98: 
 99:         if ($this->resuming) {
100:             $httpResponse->setHeader('Accept-Ranges', 'bytes');
101:             if (preg_match('#^bytes=(\d*)-(\d*)\z#', $httpRequest->getHeader('Range'), $matches)) {
102:                 list(, $start, $end) = $matches;
103:                 if ($start === '') {
104:                     $start = max(0, $filesize - $end);
105:                     $end = $filesize - 1;
106: 
107:                 } elseif ($end === '' || $end > $filesize - 1) {
108:                     $end = $filesize - 1;
109:                 }
110:                 if ($end < $start) {
111:                     $httpResponse->setCode(416); // requested range not satisfiable
112:                     return;
113:                 }
114: 
115:                 $httpResponse->setCode(206);
116:                 $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
117:                 $length = $end - $start + 1;
118:                 fseek($handle, $start);
119: 
120:             } else {
121:                 $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
122:             }
123:         }
124: 
125:         $httpResponse->setHeader('Content-Length', $length);
126:         while (!feof($handle) && $length > 0) {
127:             echo $s = fread($handle, min(4e6, $length));
128:             $length -= strlen($s);
129:         }
130:         fclose($handle);
131:     }
132: 
133: }
134: 
Nette Framework 2.0.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0