public function savefieldAction() { $request = $this->getRequest(); $field_data = $request->getPost(); if (isset($_FILES["photo"]["error"])) { if ($_FILES["photo"]["error"] > 0) { if ($_FILES["photo"]["error"] != 4) { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $_FILES["photo"]["error"] . '</div>'); $this->_redirect('/admin/addfield'); } //else 4 = No file was uploaded, than do nothing. (add with default icon) } else { $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo($filename, PATHINFO_EXTENSION); if (!array_key_exists($ext, $allowed)) { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('FILE_WRONG_FORMAT') . '</div>'); $this->_redirect('/admin/addfield'); } // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if ($filesize > $maxsize) { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('FILE_SIZE_LIMIT') . '</div>'); $this->_redirect('/admin/addfield'); } // Verify MYME type of the file if (in_array($filetype, $allowed)) { // Check whether file exists before uploading it if (!file_exists($this->config->paths->upload->fields . $_FILES["photo"]["name"])) { move_uploaded_file($_FILES["photo"]["tmp_name"], $this->config->paths->upload->fields . $_FILES["photo"]["name"]); } } else { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('FILE_ERROR') . '</div>'); $this->_redirect('/admin/addfield'); } } } else { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('FILE_ERROR') . '</div>'); $this->_redirect('/admin/addfield'); } $field_DB = new Application_Model_DbTable_Field(); $fieldName = trim($field_data['fieldName']); if (!strlen($fieldName)) { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('REQUIRED_FIELD_NAME') . '</div>'); $this->_redirect('/admin/addfield'); } else { if (count($field_DB->isExists($field_data['fieldName'])) > 0) { $this->msger->addMessage('<div class="alert alert-danger text-center" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->lang->_('FIELD_NAME_EXISTS') . '</div>'); $this->_redirect('/admin/addfield'); } } $new_field = array('name' => $field_data['fieldName'], 'icon' => $_FILES["photo"]["name"]); try { $field_id = $field_DB->insert($new_field); } catch (Exception $ex) { die(json_encode(array('status' => 'danger', 'msg' => $ex->getMessage()))); } $this->_redirect("/admin/fields"); }