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: 31:
32: class HttpUploadedFile extends Object
33: {
34:
35: private $name;
36:
37:
38: private $type;
39:
40:
41: private $size;
42:
43:
44: private $tmpName;
45:
46:
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;
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: 69: 70:
71: public function getName()
72: {
73: return $this->name;
74: }
75:
76:
77:
78: 79: 80: 81:
82: public function getSanitizedName()
83: {
84: return trim(Strings::webalize($this->name, '.', FALSE), '.-');
85: }
86:
87:
88:
89: 90: 91: 92:
93: public function getContentType()
94: {
95: if ($this->isOk() && $this->type === NULL) {
96: $this->type = MimeTypeDetector::fromFile($this->tmpName);
97: }
98: return $this->type;
99: }
100:
101:
102:
103: 104: 105: 106:
107: public function getSize()
108: {
109: return $this->size;
110: }
111:
112:
113:
114: 115: 116: 117:
118: public function getTemporaryFile()
119: {
120: return $this->tmpName;
121: }
122:
123:
124:
125: 126: 127: 128:
129: public function __toString()
130: {
131: return $this->tmpName;
132: }
133:
134:
135:
136: 137: 138: 139:
140: public function getError()
141: {
142: return $this->error;
143: }
144:
145:
146:
147: 148: 149: 150:
151: public function isOk()
152: {
153: return $this->error === UPLOAD_ERR_OK;
154: }
155:
156:
157:
158: 159: 160: 161: 162:
163: public function move($dest)
164: {
165: @mkdir(dirname($dest), 0777, TRUE);
166: if (substr(PHP_OS, 0, 3) === 'WIN') { @unlink($dest); }
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: 179: 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: 190: 191:
192: public function toImage()
193: {
194: return Image::fromFile($this->tmpName);
195: }
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:
214: public function getContents()
215: {
216:
217: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
218: }
219:
220: }
221: