Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NFtp
  • NHtml
  • NHttpContext
  • NHttpRequest
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionNamespace
  • NUri
  • NUriScript
  • NUser

Interfaces

  • IHttpRequest
  • IHttpResponse
  • IUser

Exceptions

  • NFtpException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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\Web
 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 NImage $image
 25:  * @property-read int $error
 26:  * @property-read array $imageSize
 27:  * @property-read bool $ok
 28:  * @package Nette\Web
 29:  */
 30: class NHttpUploadedFile extends NObject
 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 MIME content type of an uploaded file.
 78:      * @return string
 79:      */
 80:     public function getContentType()
 81:     {
 82:         if ($this->isOk() && $this->type === NULL) {
 83:             $this->type = NTools::detectMimeType($this->tmpName);
 84:         }
 85:         return $this->type;
 86:     }
 87: 
 88: 
 89: 
 90:     /**
 91:      * Returns the size of an uploaded file.
 92:      * @return int
 93:      */
 94:     public function getSize()
 95:     {
 96:         return $this->size;
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * Returns the path to an uploaded file.
103:      * @return string
104:      */
105:     public function getTemporaryFile()
106:     {
107:         return $this->tmpName;
108:     }
109: 
110: 
111: 
112:     /**
113:      * Returns the path to an uploaded file.
114:      * @return string
115:      */
116:     public function __toString()
117:     {
118:         return $this->tmpName;
119:     }
120: 
121: 
122: 
123:     /**
124:      * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
125:      * @return int
126:      */
127:     public function getError()
128:     {
129:         return $this->error;
130:     }
131: 
132: 
133: 
134:     /**
135:      * Is there any error?
136:      * @return bool
137:      */
138:     public function isOk()
139:     {
140:         return $this->error === UPLOAD_ERR_OK;
141:     }
142: 
143: 
144: 
145:     /**
146:      * Move uploaded file to new location.
147:      * @param  string
148:      * @return NHttpUploadedFile  provides a fluent interface
149:      */
150:     public function move($dest)
151:     {
152:         $dir = dirname($dest);
153:         if (@mkdir($dir, 0755, TRUE)) { // @ - $dir may already exist
154:             chmod($dir, 0755);
155:         }
156:         $func = is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename';
157:         if (substr(PHP_OS, 0, 3) === 'WIN') { @unlink($dest); }
158:         if (!$func($this->tmpName, $dest)) {
159:             throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
160:         }
161:         chmod($dest, 0644);
162:         $this->tmpName = $dest;
163:         return $this;
164:     }
165: 
166: 
167: 
168:     /**
169:      * Is uploaded file GIF, PNG or JPEG?
170:      * @return bool
171:      */
172:     public function isImage()
173:     {
174:         return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
175:     }
176: 
177: 
178: 
179:     /**
180:      * Returns the image.
181:      * @return NImage
182:      */
183:     public function getImage()
184:     {
185:         return NImage::fromFile($this->tmpName);
186:     }
187: 
188: 
189: 
190:     /**
191:      * Returns the dimensions of an uploaded image as array.
192:      * @return array
193:      */
194:     public function getImageSize()
195:     {
196:         return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
197:     }
198: 
199: }
200: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0