Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 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:  */
 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: class FileResponse extends Nette\Object implements Nette\Application\IResponse
 24: {
 25:     /** @var string */
 26:     private $file;
 27: 
 28:     /** @var string */
 29:     private $contentType;
 30: 
 31:     /** @var string */
 32:     private $name;
 33: 
 34:     /** @var bool */
 35:     public $resuming = TRUE;
 36: 
 37: 
 38:     /**
 39:      * @param  string  file path
 40:      * @param  string  user name name
 41:      * @param  string  MIME content type
 42:      */
 43:     public function __construct($file, $name = NULL, $contentType = NULL)
 44:     {
 45:         if (!is_file($file)) {
 46:             throw new Nette\Application\BadRequestException("File '$file' doesn't exist.");
 47:         }
 48: 
 49:         $this->file = $file;
 50:         $this->name = $name ? $name : basename($file);
 51:         $this->contentType = $contentType ? $contentType : 'application/octet-stream';
 52:     }
 53: 
 54: 
 55: 
 56:     /**
 57:      * Returns the path to a downloaded file.
 58:      * @return string
 59:      */
 60:     final public function getFile()
 61:     {
 62:         return $this->file;
 63:     }
 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:     /**
 79:      * Returns the MIME content type of a downloaded file.
 80:      * @return string
 81:      */
 82:     final public function getContentType()
 83:     {
 84:         return $this->contentType;
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Sends response to output.
 91:      * @return void
 92:      */
 93:     public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 94:     {
 95:         $httpResponse->setContentType($this->contentType);
 96:         $httpResponse->setHeader('Content-Disposition', 'attachment; 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:             $range = $httpRequest->getHeader('Range');
104:             if ($range !== NULL) {
105:                 $range = substr($range, 6); // 6 == strlen('bytes=')
106:                 list($start, $end) = explode('-', $range);
107:                 if ($start == NULL) {
108:                     $start = 0;
109:                 }
110:                 if ($end == NULL) {
111:                     $end = $filesize - 1;
112:                 }
113: 
114:                 if ($start < 0 || $end <= $start || $end > $filesize -1) {
115:                     $httpResponse->setCode(416); // requested range not satisfiable
116:                     return;
117:                 }
118: 
119:                 $httpResponse->setCode(206);
120:                 $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
121:                 $length = $end - $start + 1;
122:                 fseek($handle, $start);
123: 
124:             } else {
125:                 $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
126:             }
127:         }
128: 
129:         $httpResponse->setHeader('Content-Length', $length);
130:         while (!feof($handle)) {
131:             echo fread($handle, 4e6);
132:         }
133:         fclose($handle);
134:     }
135: 
136: }
137: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0