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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  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 extends Nette\Object
 26: {
 27:     /** @var string */
 28:     private $name;
 29: 
 30:     /** @var string */
 31:     private $type;
 32: 
 33:     /** @var string */
 34:     private $size;
 35: 
 36:     /** @var string */
 37:     private $tmpName;
 38: 
 39:     /** @var int */
 40:     private $error;
 41: 
 42: 
 43:     public function __construct($value)
 44:     {
 45:         foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
 46:             if (!isset($value[$key]) || !is_scalar($value[$key])) {
 47:                 $this->error = UPLOAD_ERR_NO_FILE;
 48:                 return; // or throw exception?
 49:             }
 50:         }
 51:         $this->name = $value['name'];
 52:         $this->size = $value['size'];
 53:         $this->tmpName = $value['tmp_name'];
 54:         $this->error = $value['error'];
 55:     }
 56: 
 57: 
 58:     /**
 59:      * Returns the file name.
 60:      * @return string
 61:      */
 62:     public function getName()
 63:     {
 64:         return $this->name;
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Returns the sanitized file name.
 70:      * @return string
 71:      */
 72:     public function getSanitizedName()
 73:     {
 74:         return trim(Nette\Utils\Strings::webalize($this->name, '.', FALSE), '.-');
 75:     }
 76: 
 77: 
 78:     /**
 79:      * Returns the MIME content type of an uploaded file.
 80:      * @return string|NULL
 81:      */
 82:     public function getContentType()
 83:     {
 84:         if ($this->isOk() && $this->type === NULL) {
 85:             $this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->tmpName);
 86:         }
 87:         return $this->type;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Returns the size of an uploaded file.
 93:      * @return int
 94:      */
 95:     public function getSize()
 96:     {
 97:         return $this->size;
 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:      * Returns the path to an uploaded file.
113:      * @return string
114:      */
115:     public function __toString()
116:     {
117:         return (string) $this->tmpName;
118:     }
119: 
120: 
121:     /**
122:      * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
123:      * @return int
124:      */
125:     public function getError()
126:     {
127:         return $this->error;
128:     }
129: 
130: 
131:     /**
132:      * Is there any error?
133:      * @return bool
134:      */
135:     public function isOk()
136:     {
137:         return $this->error === UPLOAD_ERR_OK;
138:     }
139: 
140: 
141:     /**
142:      * Move uploaded file to new location.
143:      * @param  string
144:      * @return static
145:      */
146:     public function move($dest)
147:     {
148:         @mkdir(dirname($dest), 0777, TRUE); // @ - dir may already exist
149:         @unlink($dest); // @ - file may not exists
150:         if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
151:             throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
152:         }
153:         @chmod($dest, 0666); // @ - possible low permission to chmod
154:         $this->tmpName = $dest;
155:         return $this;
156:     }
157: 
158: 
159:     /**
160:      * Is uploaded file GIF, PNG or JPEG?
161:      * @return bool
162:      */
163:     public function isImage()
164:     {
165:         return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
166:     }
167: 
168: 
169:     /**
170:      * Returns the image.
171:      * @return Nette\Utils\Image
172:      * @throws Nette\Utils\ImageException
173:      */
174:     public function toImage()
175:     {
176:         return Nette\Utils\Image::fromFile($this->tmpName);
177:     }
178: 
179: 
180:     /**
181:      * Returns the dimensions of an uploaded image as array.
182:      * @return array|NULL
183:      */
184:     public function getImageSize()
185:     {
186:         return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
187:     }
188: 
189: 
190:     /**
191:      * Get file contents.
192:      * @return string|NULL
193:      */
194:     public function getContents()
195:     {
196:         // future implementation can try to work around safe_mode and open_basedir limitations
197:         return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
198:     }
199: 
200: }
201: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0