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