<?php

use Aws\S3\Exception\S3Exception;
require '../extentions/aws/start.php';


if (isset($_FILES['file'])) {
  // code...
  $file = $_FILES['file'];

  $name = $file['name'];
  $tmp_name=$file['tmp_name'];
  $extension = explode('.',$name);

  $extension = strtolower(end($extension));

  $key = md5(uniqid());
  $tmp_file_name = "{$key}.{$extension}";
  $tmp_file_path = "tmpFiles/{$tmp_file_name}";
//var_dump($tmp_file_path);

  move_uploaded_file($tmp_name,$tmp_file_path);

  try {

    $s3->putObject([

      'Bucket' => $config['s3']['bucket'],
      'Key' => "uploads/{$name}",
      'Body' => fopen($tmp_file_path,'rb'),
      'ACL' => 'public-read'

    ]);

    unlink($tmp_file_path);

  } catch (S3Exception $e) {
    die("Hubo un error subiendo el archivo");
  }

}
 ?>
 <!DOCTYPE html>
 <html lang="en" dir="ltr">
   <head>
     <meta charset="utf-8">
     <title>Upload</title>
   </head>
   <body>
     <form action="upload2.php" method="post" enctype="multipart/form-data">
       <input type="file" name="file" value="">
       <input type="submit" name="" value="Upload">

     </form>
   </body>
 </html>
