public function save() { $uploadStatus; if (isset($_FILES['file'])) { require APP . "lib/UploadPicture.php"; //? Separate Upload Logic into service ? $upload1 = new UploadPicture(); $upload1->setSavePath(rtrim(ROOT, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR); $upload1->setDesiredSize(300, 300); try { // savePicture() returns full path to the image $savedFileName = $upload1->savePicture($_FILES['file'], null, true, 50); // Store the path URL into the User database $this->model->insertUserAvatarUrl($_SESSION['valid_user'], $savedFileName); // Redirect to the home page header('location: ' . URL . 'home'); } catch (Exception $exc) { // Where would the message go if fail? $uploadStatus = $exc->getMessage(); } } else { // Save the Error message for display $uploadStatus = 'No File was selected <br/>'; } // require the upload status view // Invoked if there is any error require TEMPLATES_PATH . "head.php"; require APP . 'views/uploadFile.php'; require TEMPLATES_PATH . "head.php"; require TEMPLATES_PATH . "navigation.php"; }
/** * admin_add method * * @return void */ public function admin_add() { $this->layout = "admin"; if ($this->request->is('post')) { $fotoRes = ""; try { if ($this->request->data["Productobase"]["foto"]) { if (!empty(trim($this->request->data["Productobase"]["foto"]["name"])) && !trim($this->request->data["Productobase"]["foto"]["name"]) !== 'e') { $upload = new UploadPicture(); $upload->setSavePath("img/fotos"); $name = strtoupper($this->getRandomKey(25)["pass"]); $foto = $this->request->data["Productobase"]["foto"]; $fotoRes = $upload->savePicture($foto, $name); // debug($fotoRes); } else { $this->Productobase->validator()->remove('foto'); } } else { $this->Productobase->validator()->remove('foto'); } } catch (Exception $ex) { $this->Flash->error(__('El producto no fue guardado1.')); return; } //Agregar transacciń $this->request->data["Productobase"]["foto"] = $fotoRes; $variedades = $this->request->data["Productobase"]["variedades"]; $variedades = explode(",", $variedades); $va = array(); foreach ($variedades as $variedad) { $va["Variedad"]["nombre"] = $variedad; } $va["Productobase"] = $this->request->data["Productobase"]; //debug($va); // exit; //$this->Productobase->create(); if ($this->Productobase->saveAll($va)) { $this->Flash->success(__('El producto fue guardado.')); //return $this->redirect(array('action' => 'index')); } else { $this->Flash->error(__('El producto no fue guardado.')); } } $variedads = $this->Productobase->Variedad->find('list'); $this->set(compact('variedads')); }
<?php include "UploadPicture.php"; //save just image without any other parameters //this usage will save file in current directory at given size and without compression $upload = new UploadPicture(); try { $upload->savePicture($_FILES['file']); } catch (\Exception $exc) { //do what you want in case of error } //save file in desired directory (for example in a directory "pictures" on a superior level $upload1 = new UploadPicture(); $upload1->setSavePath(rtrim(__DIR__, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'pictures' . DIRECTORY_SEPARATOR); try { $upload1->savePicture($_FILES['file']); } catch (\Exception $exc) { //do what you want in case of error } //save file at desired size $upload2 = new UploadPicture(); $upload2->setDesiredSize(300, 300); try { $upload2->savePicture($_FILES['file'], null, true); } catch (\Exception $exc) { //do what you want in case of error } //save file with desired name, with desired size and at compression of 90 //compression of file offer a good usage of disk $upload3 = new UploadPicture(); $upload3->setDesiredSize(500, 300);
public function ajaxAdd($currentModel, $userData, $currentData, $tipoUsuario, $currentData1 = null) { $this->layout = null; $this->autoRender = false; $this->User->recursive = -1; $this->loadModel($currentModel); $this->{$currentModel}->recursive = -1; //Recursividad $data["res"] = "no"; //$dataCurrentModel = new stdClass(); $errores = array(); $this->User->set($userData); $this->{$currentModel}->set($currentData); $fotoRes = ""; try { if ($userData["foto"]) { if (!empty(trim($userData["foto"]["name"])) && !trim($userData["foto"]["name"]) !== 'e') { $upload = new UploadPicture(); $upload->setSavePath("img/fotos"); $name = strtoupper($this->getRandomKey(25)["pass"]); $foto = $userData["foto"]; $fotoRes = $upload->savePicture($foto, $name); // debug($fotoRes); } else { $this->User->validator()->remove('foto'); } } else { $this->User->validator()->remove('foto'); } } catch (Exception $ex) { $data["msj"] = $ex->getMessage(); goto finAjaxAdminAdd; } // debug($fotoRes); $userData["foto"] = $fotoRes; //***************************************************************************** if (!$this->User->validates()) { $errores[0] = $this->User->validationErrors; } if (!$this->{$currentModel}->validates()) { $errores[1] = $this->{$currentModel}->validationErrors; } // debug($this->request->data); //Almacenamiento de errores if (count($errores) > 0) { foreach ($errores as $v) { foreach ($v as $key => $value) { // debug($errores[]); foreach ($value as $val) { $data["errores_validacion"][$key] = $val; } } } $data["msj"] = "El usuario no fue registrado, intenta de nuevo."; goto finAjaxAdminAdd; return; //Por si el goto no funciona } //****************************** Fin Validaciones ************************************ //Obtención del rol $rol = $this->obtenerRol($tipoUsuario); if ($rol === 0) { $data["msj"] = "Error al intentar asignar el tipo de usuario."; goto finAjaxAdminAdd; return; //Por si el goto no funciona } //$this->{$userModel}->create(); $userData["rol_id"] = $rol["id"]; $userData["foto"] = $fotoRes; $d["User"] = $userData; $d[$currentModel] = $currentData; $true = false; //Insercción de certificaciones para el agricultor if (count($currentData1) > 0 && isset($currentData1["id"])) { $true = true; } else { $true = false; } //debug($d); exit; $dataSource = $this->User->getDataSource(); $dataSource->begin(); if ($this->{$currentModel}->saveAll($d)) { //Certificaciones agricultor if ($true === true) { //debug($d); exit; $this->loadModel("AgricultorCertificacion"); foreach ($currentData1["id"] as $value) { $this->AgricultorCertificacion->create(); $agrCer = array(); $agrCer["AgricultorCertificacion"]["agricultor_id"] = $this->{$currentModel}->getInsertID(); $agrCer["AgricultorCertificacion"]["certificacion_id"] = $value; if ($this->AgricultorCertificacion->save($agrCer)) { $dataSource->commit(); $data["res"] = "si"; $data["msj"] = "El usuario fue registrado."; } else { //debug($agrCer); //debug($this->User->inserted_ids); $dataSource->rollback(); $data["res"] = "no"; $data["msj"] = "El usuario no fue registrado, error al intentar registrar las certificaciones"; } //debug($agrCer); } } else { $dataSource->commit(); $data["res"] = "si"; $data["msj"] = "El usuario fue registrado."; } } else { //debug($this->{$userModel}->getDataSource()->getLog(false, false)); //show last sql query $data["msj"] = "El usuario no fue registrado, intenta de nuevo."; $dataSource->rollback(); } finAjaxAdminAdd: echo json_encode($data); }