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 NHttpUploadedFile extends NObject
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 getContentType()
81: {
82: if ($this->isOk() && $this->type === NULL) {
83: $this->type = NTools::detectMimeType($this->tmpName);
84: }
85: return $this->type;
86: }
87:
88:
89:
90: 91: 92: 93:
94: public function getSize()
95: {
96: return $this->size;
97: }
98:
99:
100:
101: 102: 103: 104:
105: public function getTemporaryFile()
106: {
107: return $this->tmpName;
108: }
109:
110:
111:
112: 113: 114: 115:
116: public function __toString()
117: {
118: return $this->tmpName;
119: }
120:
121:
122:
123: 124: 125: 126:
127: public function getError()
128: {
129: return $this->error;
130: }
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:
150: public function move($dest)
151: {
152: $dir = dirname($dest);
153: if (@mkdir($dir, 0755, TRUE)) {
154: chmod($dir, 0755);
155: }
156: $func = is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename';
157: if (substr(PHP_OS, 0, 3) === 'WIN') { @unlink($dest); }
158: if (!$func($this->tmpName, $dest)) {
159: throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
160: }
161: chmod($dest, 0644);
162: $this->tmpName = $dest;
163: return $this;
164: }
165:
166:
167:
168: 169: 170: 171:
172: public function isImage()
173: {
174: return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
175: }
176:
177:
178:
179: 180: 181: 182:
183: public function getImage()
184: {
185: return NImage::fromFile($this->tmpName);
186: }
187:
188:
189:
190: 191: 192: 193:
194: public function getImageSize()
195: {
196: return $this->isOk() ? @getimagesize($this->tmpName) : NULL;
197: }
198:
199: }
200: