ayand04

uploadHandler

Mar 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Handles the image uploads
  4.  * Author: Ayan Dey
  5.  */
  6.  
  7. error_reporting(E_WARNING | E_PARSE);
  8.  
  9. require "dbClass.php";
  10. $db = new DB();
  11.  
  12. date_default_timezone_set('America/Los_Angeles');
  13. $timeStamp = time();
  14.  
  15. // Upload path
  16. $path = "../uploads/";
  17.  
  18. if($_SERVER["REQUEST_METHOD"] === "POST") {
  19.     $error = "";
  20.     $message = "";
  21.     $name = filter_var($_POST["heading"], FILTER_SANITIZE_STRING);
  22.  
  23.     if(empty($name)) {
  24.         $error = "Please type your name..";
  25.     }
  26.     else {
  27.         if($_FILES["fileUpload"]["error"] === 0) {
  28.             $filename = $_FILES["fileUpload"]["name"];
  29.             $fileExt = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  30.  
  31.             if($fileExt === "jpg") {
  32.                 $tmpFile = $_FILES["fileUpload"]["tmp_name"];
  33.                 $newFile = preg_replace('/\s+/', '', $name) . "_" . $timeStamp . "." . $fileExt;
  34.  
  35.                 if(move_uploaded_file($tmpFile, $path . $newFile)) {
  36.                     $db->query("INSERT INTO files(name, filename) VALUES(:name, :filename)", array("name"=> $name, "filename"=> $newFile));
  37.                     imageFixOrientation($path . $newFile);
  38.                     $message = "File Upload Successful";
  39.                 }
  40.                 else {
  41.                     $error = "Failed to upload the snap..";
  42.                 }
  43.             }
  44.             else {
  45.                 $error = "Wrong file type!";
  46.             }
  47.         }
  48.         elseif ($_FILES["fileUpload"]["error"] === 4) {
  49.             $error = "No file selected..";
  50.         }
  51.         else {
  52.             $error = "Some error occured while uploading the file. Error Code: ".$_FILES["fileUpload"]["error"];
  53.         }
  54.     }
  55.  
  56.     if(empty($error)) {
  57.         echo json_encode(array("MSG" => $message, "CLASS" => "success", "code" => 0));
  58.     }
  59.     else {
  60.         echo json_encode(array("MSG" => $error, "CLASS" => "error", "code" => 1));
  61.     }
  62. }
  63.  
  64. function imageFixOrientation($filename) {
  65.     if (function_exists('exif_read_data')) {
  66.         $exif = exif_read_data($filename);
  67.         if($exif && isset($exif['Orientation'])) {
  68.             $orientation = $exif['Orientation'];
  69.             if($orientation != 1){
  70.                 $img = imagecreatefromjpeg($filename);
  71.                 $deg = 0;
  72.                 switch ($orientation) {
  73.                     case 3:
  74.                         $deg = 180;
  75.                         break;
  76.                     case 6:
  77.                         $deg = 270;
  78.                         break;
  79.                     case 8:
  80.                         $deg = 90;
  81.                         break;
  82.                 }
  83.                 if ($deg) {
  84.                     $img = imagerotate($img, $deg, 0);
  85.                 }
  86.                 // then rewrite the rotated image back to the disk as $filename
  87.                 imagejpeg($img, $filename, 95);
  88.             } // if there is some rotation necessary
  89.         } // if have the exif orientation info
  90.     } // if function exists
  91. }
Add Comment
Please, Sign In to add comment