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: * @package Nette\Http
11: */
12:
13:
14:
15: /**
16: * Provides access to individual files that have been uploaded by a client.
17: *
18: * @author David Grudl
19: *
20: * @property-read string $name
21: * @property-read string $sanitizedName
22: * @property-read string $contentType
23: * @property-read int $size
24: * @property-read string $temporaryFile
25: * @property-read int $error
26: * @property-read bool $ok
27: * @property-read bool $image
28: * @property-read array $imageSize
29: * @property-read string $contents
30: * @package Nette\Http
31: */
32: class HttpUploadedFile extends 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: public function __construct($value)
51: {
52: foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
53: if (!isset($value[$key]) || !is_scalar($value[$key])) {
54: $this->error = UPLOAD_ERR_NO_FILE;
55: return; // or throw exception?
56: }
57: }
58: $this->name = $value['name'];
59: $this->size = $value['size'];
60: $this->tmpName = $value['tmp_name'];
61: $this->error = $value['error'];
62: }
63:
64:
65: /**
66: * Returns the file name.
67: * @return string
68: */
69: public function getName()
70: {
71: return $this->name;
72: }
73:
74:
75: /**
76: * Returns the sanitized file name.
77: * @return string
78: */
79: public function getSanitizedName()
80: {
81: return trim(Strings::webalize($this->name, '.', FALSE), '.-');
82: }
83:
84:
85: /**
86: * Returns the MIME content type of an uploaded file.
87: * @return string
88: */
89: public function getContentType()
90: {
91: if ($this->isOk() && $this->type === NULL) {
92: $this->type = MimeTypeDetector::fromFile($this->tmpName);
93: }
94: return $this->type;
95: }
96:
97:
98: /**
99: * Returns the size of an uploaded file.
100: * @return int
101: */
102: public function getSize()
103: {
104: return $this->size;
105: }
106:
107:
108: /**
109: * Returns the path to an uploaded file.
110: * @return string
111: */
112: public function getTemporaryFile()
113: {
114: return $this->tmpName;
115: }
116:
117:
118: /**
119: * Returns the path to an uploaded file.
120: * @return string
121: */
122: public function __toString()
123: {
124: return $this->tmpName;
125: }
126:
127:
128: /**
129: * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
130: * @return int
131: */
132: public function getError()
133: {
134: return $this->error;
135: }
136:
137:
138: /**
139: * Is there any error?
140: * @return bool
141: */
142: public function isOk()
143: {
144: return $this->error === UPLOAD_ERR_OK;
145: }
146:
147:
148: /**
149: * Move uploaded file to new location.
150: * @param string
151: * @return self
152: */
153: public function move($dest)
154: {
155: @mkdir(dirname($dest), 0777, TRUE); // @ - dir may already exist
156: @unlink($dest); // @ - file may not exists
157: if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
158: throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
159: }
160: chmod($dest, 0666);
161: $this->tmpName = $dest;
162: return $this;
163: }
164:
165:
166: /**
167: * Is uploaded file GIF, PNG or JPEG?
168: * @return bool
169: */
170: public function isImage()
171: {
172: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
173: }
174:
175:
176: /**
177: * Returns the image.
178: * @return Image
179: */
180: public function toImage()
181: {
182: return Image::fromFile($this->tmpName);
183: }
184:
185:
186: /**
187: * Returns the dimensions of an uploaded image as array.
188: * @return array
189: */
190: public function getImageSize()
191: {
192: return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
193: }
194:
195:
196: /**
197: * Get file contents.
198: * @return string
199: */
200: public function getContents()
201: {
202: // future implementation can try to work around safe_mode and open_basedir limitations
203: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
204: }
205:
206: }
207: