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: class NHttpUploadedFile extends NObject
29: {
30:
31: private $name;
32:
33:
34: private $type;
35:
36:
37: private $size;
38:
39:
40: private $tmpName;
41:
42:
43: private $error;
44:
45:
46: public function __construct($value)
47: {
48: foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
49: if (!isset($value[$key]) || !is_scalar($value[$key])) {
50: $this->error = UPLOAD_ERR_NO_FILE;
51: return;
52: }
53: }
54: $this->name = $value['name'];
55: $this->size = $value['size'];
56: $this->tmpName = $value['tmp_name'];
57: $this->error = $value['error'];
58: }
59:
60:
61: 62: 63: 64:
65: public function getName()
66: {
67: return $this->name;
68: }
69:
70:
71: 72: 73: 74:
75: public function getSanitizedName()
76: {
77: return trim(NStrings::webalize($this->name, '.', FALSE), '.-');
78: }
79:
80:
81: 82: 83: 84:
85: public function getContentType()
86: {
87: if ($this->isOk() && $this->type === NULL) {
88: $this->type = NMimeTypeDetector::fromFile($this->tmpName);
89: }
90: return $this->type;
91: }
92:
93:
94: 95: 96: 97:
98: public function getSize()
99: {
100: return $this->size;
101: }
102:
103:
104: 105: 106: 107:
108: public function getTemporaryFile()
109: {
110: return $this->tmpName;
111: }
112:
113:
114: 115: 116: 117:
118: public function __toString()
119: {
120: return (string) $this->tmpName;
121: }
122:
123:
124: 125: 126: 127:
128: public function getError()
129: {
130: return $this->error;
131: }
132:
133:
134: 135: 136: 137:
138: public function isOk()
139: {
140: return $this->error === UPLOAD_ERR_OK;
141: }
142:
143:
144: 145: 146: 147: 148:
149: public function move($dest)
150: {
151: @mkdir(dirname($dest), 0777, TRUE);
152: @unlink($dest);
153: if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
154: throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
155: }
156: chmod($dest, 0666);
157: $this->tmpName = $dest;
158: return $this;
159: }
160:
161:
162: 163: 164: 165:
166: public function isImage()
167: {
168: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
169: }
170:
171:
172: 173: 174: 175:
176: public function toImage()
177: {
178: return NImage::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: