<?php

  header("Access-Control-Allow-Origin: *");

  if(isset($_FILES['image-file'])){
    $file = $_FILES['image-file'];
    $fileName = $file['name'];
    $fileTmp = $file['tmp_name'];
    $fileError = $file['error'];
    $fileSize = $file['size'];
    
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
    $allowed = array('jpg', 'jpeg', 'png');
    
    if(in_array($fileActualExt, $allowed)){
      if($fileError === 0){
        if($fileSize < 1000000){
          $fileNameNew = uniqid('', true) . "." . $fileActualExt;
          $fileDestination = 'uploads/' . $fileNameNew;
          move_uploaded_file($fileTmp, $fileDestination);
          echo json_encode(array("url" => "https://app.flik.com/flik-gpt/uploads/" . $fileNameNew,"status" => "success"));
        }else{
            echo json_encode(array("status" => "File too large"));

        }
      }else{
        echo json_encode(array("status" => "Error uploading file"));

      }
    }else{
        echo json_encode(array("status" => "Invalid file type"));
    }
  }
?>
