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: class HttpUploadedFile extends Object
30: {
31:
32: private $name;
33:
34:
35: private $type;
36:
37:
38: private $size;
39:
40:
41: private $tmpName;
42:
43:
44: private $error;
45:
46:
47:
48: public function __construct($value)
49: {
50: foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
51: if (!isset($value[$key]) || !is_scalar($value[$key])) {
52: $this->error = UPLOAD_ERR_NO_FILE;
53: return; 54: }
55: }
56: $this->name = $value['name'];
57: $this->size = $value['size'];
58: $this->tmpName = $value['tmp_name'];
59: $this->error = $value['error'];
60: }
61:
62:
63:
64: 65: 66: 67:
68: public function getName()
69: {
70: return $this->name;
71: }
72:
73:
74:
75: 76: 77: 78:
79: public function getContentType()
80: {
81: if ($this->isOk() && $this->type === NULL) {
82: $this->type = Tools::detectMimeType($this->tmpName);
83: }
84: return $this->type;
85: }
86:
87:
88:
89: 90: 91: 92:
93: public function getSize()
94: {
95: return $this->size;
96: }
97:
98:
99:
100: 101: 102: 103:
104: public function getTemporaryFile()
105: {
106: return $this->tmpName;
107: }
108:
109:
110:
111: 112: 113: 114:
115: public function __toString()
116: {
117: return $this->tmpName;
118: }
119:
120:
121:
122: 123: 124: 125:
126: public function getError()
127: {
128: return $this->error;
129: }
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:
149: public function move($dest)
150: {
151: $dir = dirname($dest);
152: if (@mkdir($dir, 0755, TRUE)) { 153: chmod($dir, 0755);
154: }
155: $func = is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename';
156: if (!$func($this->tmpName, $dest)) {
157: throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
158: }
159: chmod($dest, 0644);
160: $this->tmpName = $dest;
161: return $this;
162: }
163:
164:
165:
166: 167: 168: 169:
170: public function isImage()
171: {
172: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
173: }
174:
175:
176:
177: 178: 179: 180:
181: public function toImage()
182: {
183: return Image::fromFile($this->tmpName);
184: }
185:
186:
187:
188: 189: 190: 191:
192: public function getImageSize()
193: {
194: return $this->isOk() ? @getimagesize($this->tmpName) : NULL; 195: }
196:
197:
198:
199: 200: 201: 202:
203: public function getContents()
204: {
205: 206: return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
207: }
208:
209: }
210: