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