Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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:  * @package Nette\Application\Responses
 20:  */
 21: class FileResponse extends Object implements IPresenterResponse
 22: {
 23:     /** @var string */
 24:     private $file;
 25: 
 26:     /** @var string */
 27:     private $contentType;
 28: 
 29:     /** @var string */
 30:     private $name;
 31: 
 32:     /** @var bool */
 33:     public $resuming = TRUE;
 34: 
 35: 
 36:     /**
 37:      * @param  string  file path
 38:      * @param  string  user name name
 39:      * @param  string  MIME content type
 40:      */
 41:     public function __construct($file, $name = NULL, $contentType = NULL)
 42:     {
 43:         if (!is_file($file)) {
 44:             throw new BadRequestException("File '$file' doesn't exist.");
 45:         }
 46: 
 47:         $this->file = $file;
 48:         $this->name = $name ? $name : basename($file);
 49:         $this->contentType = $contentType ? $contentType : 'application/octet-stream';
 50:     }
 51: 
 52: 
 53: 
 54:     /**
 55:      * Returns the path to a downloaded file.
 56:      * @return string
 57:      */
 58:     final public function getFile()
 59:     {
 60:         return $this->file;
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * Returns the file name.
 67:      * @return string
 68:      */
 69:     final public function getName()
 70:     {
 71:         return $this->name;
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns the MIME content type of a downloaded file.
 78:      * @return string
 79:      */
 80:     final public function getContentType()
 81:     {
 82:         return $this->contentType;
 83:     }
 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:             $range = $httpRequest->getHeader('Range');
102:             if ($range !== NULL) {
103:                 $range = substr($range, 6); // 6 == strlen('bytes=')
104:                 list($start, $end) = explode('-', $range);
105:                 if ($start == NULL) {
106:                     $start = 0;
107:                 }
108:                 if ($end == NULL) {
109:                     $end = $filesize - 1;
110:                 }
111: 
112:                 if ($start < 0 || $end <= $start || $end > $filesize -1) {
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)) {
129:             echo fread($handle, 4e6);
130:         }
131:         fclose($handle);
132:     }
133: 
134: }
135: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0