Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Context
  • FileUpload
  • Helpers
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Http;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Provides access to individual files that have been uploaded by a client.
 15:  *
 16:  * @property-read string $name
 17:  * @property-read string $sanitizedName
 18:  * @property-read string|null $contentType
 19:  * @property-read int $size
 20:  * @property-read string $temporaryFile
 21:  * @property-read int $error
 22:  * @property-read bool $ok
 23:  * @property-read string|null $contents
 24:  */
 25: class FileUpload
 26: {
 27:     use Nette\SmartObject;
 28: 
 29:     /** @var string */
 30:     private $name;
 31: 
 32:     /** @var string */
 33:     private $type;
 34: 
 35:     /** @var int */
 36:     private $size;
 37: 
 38:     /** @var string */
 39:     private $tmpName;
 40: 
 41:     /** @var int */
 42:     private $error;
 43: 
 44: 
 45:     public function __construct($value)
 46:     {
 47:         foreach (['name', 'size', 'tmp_name', 'error'] as $key) {
 48:             if (!isset($value[$key]) || !is_scalar($value[$key])) {
 49:                 $this->error = UPLOAD_ERR_NO_FILE;
 50:                 return; // or throw exception?
 51:             }
 52:         }
 53:         $this->name = $value['name'];
 54:         $this->size = $value['size'];
 55:         $this->tmpName = $value['tmp_name'];
 56:         $this->error = $value['error'];
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Returns the file name.
 62:      * @return string
 63:      */
 64:     public function getName()
 65:     {
 66:         return $this->name;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Returns the sanitized file name.
 72:      * @return string
 73:      */
 74:     public function getSanitizedName()
 75:     {
 76:         return trim(Nette\Utils\Strings::webalize($this->name, '.', false), '.-');
 77:     }
 78: 
 79: 
 80:     /**
 81:      * Returns the MIME content type of an uploaded file.
 82:      * @return string|null
 83:      */
 84:     public function getContentType()
 85:     {
 86:         if ($this->isOk() && $this->type === null) {
 87:             $this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->tmpName);
 88:         }
 89:         return $this->type;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Returns the size of an uploaded file.
 95:      * @return int
 96:      */
 97:     public function getSize()
 98:     {
 99:         return $this->size;
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:      * Returns the path to an uploaded file.
115:      * @return string
116:      */
117:     public function __toString()
118:     {
119:         return (string) $this->tmpName;
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:      * Is there any error?
135:      * @return bool
136:      */
137:     public function isOk()
138:     {
139:         return $this->error === UPLOAD_ERR_OK;
140:     }
141: 
142: 
143:     /**
144:      * @return bool
145:      */
146:     public function hasFile()
147:     {
148:         return $this->error !== UPLOAD_ERR_NO_FILE;
149:     }
150: 
151: 
152:     /**
153:      * Move uploaded file to new location.
154:      * @param  string
155:      * @return static
156:      */
157:     public function move($dest)
158:     {
159:         $dir = dirname($dest);
160:         Nette\Utils\FileSystem::createDir($dir);
161:         @unlink($dest); // @ - file may not exists
162:         Nette\Utils\Callback::invokeSafe(
163:             is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename',
164:             [$this->tmpName, $dest],
165:             function ($message) use ($dest) {
166:                 throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'. $message");
167:             }
168:         );
169:         @chmod($dest, 0666); // @ - possible low permission to chmod
170:         $this->tmpName = $dest;
171:         return $this;
172:     }
173: 
174: 
175:     /**
176:      * Is uploaded file GIF, PNG or JPEG?
177:      * @return bool
178:      */
179:     public function isImage()
180:     {
181:         return in_array($this->getContentType(), ['image/gif', 'image/png', 'image/jpeg'], true);
182:     }
183: 
184: 
185:     /**
186:      * Returns the image.
187:      * @return Nette\Utils\Image
188:      * @throws Nette\Utils\ImageException
189:      */
190:     public function toImage()
191:     {
192:         return Nette\Utils\Image::fromFile($this->tmpName);
193:     }
194: 
195: 
196:     /**
197:      * Returns the dimensions of an uploaded image as array.
198:      * @return array|null
199:      */
200:     public function getImageSize()
201:     {
202:         return $this->isOk() ? @getimagesize($this->tmpName) : null; // @ - files smaller than 12 bytes causes read error
203:     }
204: 
205: 
206:     /**
207:      * Get file contents.
208:      * @return string|null
209:      */
210:     public function getContents()
211:     {
212:         // future implementation can try to work around safe_mode and open_basedir limitations
213:         return $this->isOk() ? file_get_contents($this->tmpName) : null;
214:     }
215: }
216: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0