Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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

Interfaces

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