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 NHttpUploadedFile extends NObject
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:
51: public function __construct($value)
52: {
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?
57: }
58: }
59: $this->name = $value['name'];
60: $this->size = $value['size'];
61: $this->tmpName = $value['tmp_name'];
62: $this->error = $value['error'];
63: }
64:
65:
66:
67: /**
68: * Returns the file name.
69: * @return string
70: */
71: public function getName()
72: {
73: return $this->name;
74: }
75:
76:
77:
78: /**
79: * Returns the sanitized file name.
80: * @return string
81: */
82: public function getSanitizedName()
83: {
84: return trim(NStrings::webalize($this->name, '.', FALSE), '.-');
85: }
86:
87:
88:
89: /**
90: * Returns the MIME content type of an uploaded file.
91: * @return string
92: */
93: public function getContentType()
94: {
95: if ($this->isOk() && $this->type === NULL) {
96: $this->type = NMimeTypeDetector::fromFile($this->tmpName);
97: }
98: return $this->type;
99: }
100:
101:
102:
103: /**
104: * Returns the size of an uploaded file.
105: * @return int
106: */
107: public function getSize()
108: {
109: return $this->size;
110: }
111:
112:
113:
114: /**
115: * Returns the path to an uploaded file.
116: * @return string
117: */
118: public function getTemporaryFile()
119: {
120: return $this->tmpName;
121: }
122:
123:
124:
125: /**
126: * Returns the path to an uploaded file.
127: * @return string
128: */
129: public function __toString()
130: {
131: return $this->tmpName;
132: }
133:
134:
135:
136: /**
137: * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
138: * @return int
139: */
140: public function getError()
141: {
142: return $this->error;
143: }
144:
145:
146:
147: /**
148: * Is there any error?
149: * @return bool
150: */
151: public function isOk()
152: {
153: return $this->error === UPLOAD_ERR_OK;
154: }
155:
156:
157:
158: /**
159: * Move uploaded file to new location.
160: * @param string
161: * @return NHttpUploadedFile provides a fluent interface
162: */
163: public function move($dest)
164: {
165: @mkdir(dirname($dest), 0777, TRUE); // @ - dir may already exist
166: @unlink($dest); // @ - file may not exists
167: if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
168: throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
169: }
170: chmod($dest, 0666);
171: $this->tmpName = $dest;
172: return $this;
173: }
174:
175:
176:
177: /**
178: * Is uploaded file GIF, PNG or JPEG?
179: * @return bool
180: */
181: public function isImage()
182: {
183: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
184: }
185:
186:
187:
188: /**
189: * Returns the image.
190: * @return NImage
191: */
192: public function toImage()
193: {
194: return NImage::fromFile($this->tmpName);
195: }
196:
197:
198:
199: /**
200: * Returns the dimensions of an uploaded image as array.
201: * @return array
202: */
203: public function getImageSize()
204: {
205: return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
206: }
207:
208:
209:
210: /**
211: * Get file contents.
212: * @return string
213: */
214: public function getContents()
215: {
216: // future implementation can try to work around safe_mode and open_basedir limitations
217: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
218: }
219:
220: }
221: