1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Http;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class FileUpload
26: {
27: use Nette\SmartObject;
28:
29:
30: private $name;
31:
32:
33: private $type;
34:
35:
36: private $size;
37:
38:
39: private $tmpName;
40:
41:
42: private $error;
43:
44:
45: public function __construct($value)
46: {
47: foreach (['name', 'size', 'tmp_name', 'error'] as $key) {
48: if (!isset($value[$key]) || !is_scalar($value[$key])) {
49: $this->error = UPLOAD_ERR_NO_FILE;
50: return;
51: }
52: }
53: $this->name = $value['name'];
54: $this->size = $value['size'];
55: $this->tmpName = $value['tmp_name'];
56: $this->error = $value['error'];
57: }
58:
59:
60: 61: 62: 63:
64: public function getName()
65: {
66: return $this->name;
67: }
68:
69:
70: 71: 72: 73:
74: public function getSanitizedName()
75: {
76: return trim(Nette\Utils\Strings::webalize($this->name, '.', false), '.-');
77: }
78:
79:
80: 81: 82: 83:
84: public function getContentType()
85: {
86: if ($this->isOk() && $this->type === null) {
87: $this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->tmpName);
88: }
89: return $this->type;
90: }
91:
92:
93: 94: 95: 96:
97: public function getSize()
98: {
99: return $this->size;
100: }
101:
102:
103: 104: 105: 106:
107: public function getTemporaryFile()
108: {
109: return $this->tmpName;
110: }
111:
112:
113: 114: 115: 116:
117: public function __toString()
118: {
119: return (string) $this->tmpName;
120: }
121:
122:
123: 124: 125: 126:
127: public function getError()
128: {
129: return $this->error;
130: }
131:
132:
133: 134: 135: 136:
137: public function isOk()
138: {
139: return $this->error === UPLOAD_ERR_OK;
140: }
141:
142:
143: 144: 145:
146: public function hasFile()
147: {
148: return $this->error !== UPLOAD_ERR_NO_FILE;
149: }
150:
151:
152: 153: 154: 155: 156:
157: public function move($dest)
158: {
159: $dir = dirname($dest);
160: Nette\Utils\FileSystem::createDir($dir);
161: @unlink($dest);
162: Nette\Utils\Callback::invokeSafe(
163: is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename',
164: [$this->tmpName, $dest],
165: function ($message) use ($dest) {
166: throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'. $message");
167: }
168: );
169: @chmod($dest, 0666);
170: $this->tmpName = $dest;
171: return $this;
172: }
173:
174:
175: 176: 177: 178:
179: public function isImage()
180: {
181: return in_array($this->getContentType(), ['image/gif', 'image/png', 'image/jpeg'], true);
182: }
183:
184:
185: 186: 187: 188: 189:
190: public function toImage()
191: {
192: return Nette\Utils\Image::fromFile($this->tmpName);
193: }
194:
195:
196: 197: 198: 199:
200: public function getImageSize()
201: {
202: return $this->isOk() ? @getimagesize($this->tmpName) : null;
203: }
204:
205:
206: 207: 208: 209:
210: public function getContents()
211: {
212:
213: return $this->isOk() ? file_get_contents($this->tmpName) : null;
214: }
215: }
216: