4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
18
* Provides access to individual files that have been uploaded by a client.
22
* @property-read string $name
23
* @property-read string $sanitizedName
24
* @property-read string $contentType
25
* @property-read int $size
26
* @property-read string $temporaryFile
27
* @property-read int $error
28
* @property-read bool $ok
29
* @property-read bool $image
30
* @property-read array $imageSize
31
* @property-read string $contents
33
class FileUpload extends Nette\Object
51
public function __construct($value)
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?
59
$this->name = $value['name'];
60
$this->size = $value['size'];
61
$this->tmpName = $value['tmp_name'];
62
$this->error = $value['error'];
67
* Returns the file name.
70
public function getName()
77
* Returns the sanitized file name.
80
public function getSanitizedName()
82
return trim(Nette\Utils\Strings::webalize($this->name, '.', FALSE), '.-');
87
* Returns the MIME content type of an uploaded file.
90
public function getContentType()
92
if ($this->isOk() && $this->type === NULL) {
93
$this->type = Nette\Utils\MimeTypeDetector::fromFile($this->tmpName);
100
* Returns the size of an uploaded file.
103
public function getSize()
110
* Returns the path to an uploaded file.
113
public function getTemporaryFile()
115
return $this->tmpName;
120
* Returns the path to an uploaded file.
123
public function __toString()
125
return $this->tmpName;
130
* Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
133
public function getError()
140
* Is there any error?
143
public function isOk()
145
return $this->error === UPLOAD_ERR_OK;
150
* Move uploaded file to new location.
154
public function move($dest)
156
@mkdir(dirname($dest), 0777, TRUE); // @ - dir may already exist
157
@unlink($dest); // @ - file may not exists
158
if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
159
throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
162
$this->tmpName = $dest;
168
* Is uploaded file GIF, PNG or JPEG?
171
public function isImage()
173
return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
179
* @return Nette\Image
181
public function toImage()
183
return Nette\Image::fromFile($this->tmpName);
188
* Returns the dimensions of an uploaded image as array.
191
public function getImageSize()
193
return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
201
public function getContents()
203
// future implementation can try to work around safe_mode and open_basedir limitations
204
return $this->isOk() ? file_get_contents($this->tmpName) : NULL;