public function upload($files, $limits = array()) { $parts = explode('.', $files['name'][0]); $orgfilename = $files['name'][0]; $size = $files['size'][0]; $this->ext = strtolower(strtolower(end($parts))); if ($this->allowed_exts != false && !in_array($this->ext, $this->allowed_exts)) { // Illegal file extention throw new Exception_File('Filen "' . $orgfilename . '" kunne ikke uploades. <strong>Filtypen er ikke tilladt</strong>. Kun billeder af følgende filtyper er tilladt: ' . implode(', ', $this->allowed_exts)); } if ($this->maxsize != false && (!$size || $size == 0 || $size > $this->maxsize)) { // Too big throw new Exception_File('Filen "' . $orgfilename . '" kunne ikke uploades da den er for stor! Filer må højest være ' . files::format_bytes($this->maxsize)); } if ($this->require_login && !user::logged()) { // We only accept files from logged in users throw new Exception_User('Du skal være logget ind for at uploade filer. Tjek at du er logget ind og forsøg igen.'); } $this->filename = files::randomname() . '.' . $this->ext; $i = 2; while (file_exists($this->path . $this->filename)) { $this->filename = files::randomname() . '.' . $this->ext; } try { move_uploaded_file($files['tmp_name'][0], $this->path . $this->filename); // Should throw an exception if it fails $finfo = new finfo(FILEINFO_MIME); $type = $finfo->file($this->path . $this->filename); $mime = substr($type, 0, strpos($type, ';')); if ($this->allowed_mimes != false && !in_array($mime, $this->allowed_mimes)) { // Illegal file mime throw new Exception_File('Filen "' . $orgfilename . '" kunne ikke uploades. <strong>Filtypen er ikke tilladt</strong>. Kun billeder af følgende filtyper er tilladt: ' . implode(', ', $this->allowed_exts)); } if ($this->require_login) { $this->user_id = user::get()->id; } $this->type = $mime; $this->created = time(); $this->save(); return $this; } catch (exception $e) { // File move failed. Maybe log the error? if (file_exists($this->path . $this->filename)) { unlink($this->path . $this->filename); } throw $e; } }