Example #1
0
 public function uploadImage($image, $alt)
 {
     try {
         $file = new File($image, 10000);
         $uploadDir = ASSETS . 'uploads/portfolio/gallery/';
         $tmp = ASSETS . 'uploads/portfolio/tmp/';
         $file->setUploadDir($tmp);
         $fileSaver = new FileSaver($file);
         if (!$file->isNormalSize()) {
             throw new \Exception('Very big file size');
         }
         if (!$fileSaver->save()) {
             throw new \Exception('File not selected');
         }
         if (!ImageHelper::isImage($fileSaver->uploadedFile, ['gif', 'png', 'jpg', 'jpeg'])) {
             throw new \Exception('File is not image');
         }
         if (file_exists($uploadDir . $file->getName())) {
             $uniqName = FileHelper::getUniqFileName($uploadDir, FileHelper::getFileExtension($file->getName()));
             $file->setName($uniqName);
         }
         FileHelper::move($fileSaver->uploadedFile, $uploadDir . $file->getName());
         $db = Service::get('db');
         $query = 'INSERT INTO ' . self::getTable() . '(name, alt) VALUES (:name, :alt)';
         $stmt = $db->prepare($query);
         if (!$stmt->execute([':name' => $file->getName(), ':alt' => $alt])) {
             throw new \Exception('File not saved into DB');
         }
         Service::get('session')->setFlushMsg('success', 'File successfully downloaded');
     } catch (\Exception $e) {
         Service::get('session')->setFlushMsg('error', $e->getMessage());
         $response = new ResponseRedirect(Request::getHost() . '/admin');
         $response->send();
     }
 }