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

  • HttpContext
  • HttpRequest
  • HttpRequestFactory
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionSection
  • Url
  • UrlScript
  • User

Interfaces

  • IHttpRequest
  • IHttpResponse
  • ISessionStorage
  • IUser
  • 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\Http
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Provides access to individual files that have been uploaded by a client.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read string $name
 21:  * @property-read string $contentType
 22:  * @property-read int $size
 23:  * @property-read string $temporaryFile
 24:  * @property-read Image $image
 25:  * @property-read int $error
 26:  * @property-read array $imageSize
 27:  * @property-read bool $ok
 28:  * @package Nette\Http
 29:  */
 30: class HttpUploadedFile extends Object
 31: {
 32:     /** @var string */
 33:     private $name;
 34: 
 35:     /** @var string */
 36:     private $type;
 37: 
 38:     /** @var string */
 39:     private $size;
 40: 
 41:     /** @var string */
 42:     private $tmpName;
 43: 
 44:     /** @var int */
 45:     private $error;
 46: 
 47: 
 48: 
 49:     public function __construct($value)
 50:     {
 51:         foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
 52:             if (!isset($value[$key]) || !is_scalar($value[$key])) {
 53:                 $this->error = UPLOAD_ERR_NO_FILE;
 54:                 return; // or throw exception?
 55:             }
 56:         }
 57:         $this->name = $value['name'];
 58:         $this->size = $value['size'];
 59:         $this->tmpName = $value['tmp_name'];
 60:         $this->error = $value['error'];
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * Returns the file name.
 67:      * @return string
 68:      */
 69:     public function getName()
 70:     {
 71:         return $this->name;
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns the sanitized file name.
 78:      * @return string
 79:      */
 80:     public function getSanitizedName()
 81:     {
 82:         return trim(Strings::webalize($this->name, '.', FALSE), '.-');
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * Returns the MIME content type of an uploaded file.
 89:      * @return string
 90:      */
 91:     public function getContentType()
 92:     {
 93:         if ($this->isOk() && $this->type === NULL) {
 94:             $this->type = MimeTypeDetector::fromFile($this->tmpName);
 95:         }
 96:         return $this->type;
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * Returns the size of an uploaded file.
103:      * @return int
104:      */
105:     public function getSize()
106:     {
107:         return $this->size;
108:     }
109: 
110: 
111: 
112:     /**
113:      * Returns the path to an uploaded file.
114:      * @return string
115:      */
116:     public function getTemporaryFile()
117:     {
118:         return $this->tmpName;
119:     }
120: 
121: 
122: 
123:     /**
124:      * Returns the path to an uploaded file.
125:      * @return string
126:      */
127:     public function __toString()
128:     {
129:         return $this->tmpName;
130:     }
131: 
132: 
133: 
134:     /**
135:      * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
136:      * @return int
137:      */
138:     public function getError()
139:     {
140:         return $this->error;
141:     }
142: 
143: 
144: 
145:     /**
146:      * Is there any error?
147:      * @return bool
148:      */
149:     public function isOk()
150:     {
151:         return $this->error === UPLOAD_ERR_OK;
152:     }
153: 
154: 
155: 
156:     /**
157:      * Move uploaded file to new location.
158:      * @param  string
159:      * @return HttpUploadedFile  provides a fluent interface
160:      */
161:     public function move($dest)
162:     {
163:         $dir = dirname($dest);
164:         if (@mkdir($dir, 0755, TRUE)) { // @ - $dir may already exist
165:             chmod($dir, 0755);
166:         }
167:         $func = is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename';
168:         if (substr(PHP_OS, 0, 3) === 'WIN') { @unlink($dest); }
169:         if (!$func($this->tmpName, $dest)) {
170:             throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
171:         }
172:         chmod($dest, 0644);
173:         $this->tmpName = $dest;
174:         return $this;
175:     }
176: 
177: 
178: 
179:     /**
180:      * Is uploaded file GIF, PNG or JPEG?
181:      * @return bool
182:      */
183:     public function isImage()
184:     {
185:         return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
186:     }
187: 
188: 
189: 
190:     /**
191:      * Returns the image.
192:      * @return Image
193:      */
194:     public function toImage()
195:     {
196:         return Image::fromFile($this->tmpName);
197:     }
198: 
199: 
200: 
201:     /**
202:      * Returns the dimensions of an uploaded image as array.
203:      * @return array
204:      */
205:     public function getImageSize()
206:     {
207:         return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
208:     }
209: 
210: 
211: 
212:     /**
213:      * Get file contents.
214:      * @return string
215:      */
216:     public function getContents()
217:     {
218:         // future implementation can try to work around safe_mode and open_basedir limitations
219:         return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
220:     }
221: 
222: }
223: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0