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"; }
$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); try { $upload3->savePicture($_FILES['file'], 'MyPicture.jpg', true, 90); } catch (\Exception $exc) { echo $exc->getMessage(); }