Namespaces

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

Classes

  • Ftp
  • Html
  • HttpContext
  • HttpRequest
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionNamespace
  • Uri
  • UriScript
  • User

Interfaces

  • IHttpRequest
  • IHttpResponse
  • IUser

Exceptions

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