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: 26:
27: class FileUpload extends Nette\Object
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 (array('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: 147:
148: public function move($dest)
149: {
150: @mkdir(dirname($dest), 0777, TRUE);
151: @unlink($dest);
152: if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
153: throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
154: }
155: @chmod($dest, 0666);
156: $this->tmpName = $dest;
157: return $this;
158: }
159:
160:
161: 162: 163: 164:
165: public function isImage()
166: {
167: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
168: }
169:
170:
171: 172: 173: 174: 175:
176: public function toImage()
177: {
178: return Nette\Utils\Image::fromFile($this->tmpName);
179: }
180:
181:
182: 183: 184: 185:
186: public function getImageSize()
187: {
188: return $this->isOk() ? @getimagesize($this->tmpName) : NULL;
189: }
190:
191:
192: 193: 194: 195:
196: public function getContents()
197: {
198:
199: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
200: }
201:
202: }
203: