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', 'type', '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: @mkdir($dir, 0777, TRUE);
161: if (!is_dir($dir)) {
162: throw new Nette\InvalidStateException("Directory '$dir' cannot be created. " . error_get_last()['message']);
163: }
164: @unlink($dest);
165: Nette\Utils\Callback::invokeSafe(
166: is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename',
167: [$this->tmpName, $dest],
168: function ($message) use ($dest) {
169: throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'. $message");
170: }
171: );
172: @chmod($dest, 0666);
173: $this->tmpName = $dest;
174: return $this;
175: }
176:
177:
178: 179: 180: 181:
182: public function isImage()
183: {
184: return in_array($this->getContentType(), ['image/gif', 'image/png', 'image/jpeg'], TRUE);
185: }
186:
187:
188: 189: 190: 191: 192:
193: public function toImage()
194: {
195: return Nette\Utils\Image::fromFile($this->tmpName);
196: }
197:
198:
199: 200: 201: 202:
203: public function getImageSize()
204: {
205: return $this->isOk() ? @getimagesize($this->tmpName) : NULL;
206: }
207:
208:
209: 210: 211: 212:
213: public function getContents()
214: {
215:
216: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
217: }
218:
219: }
220: