<?php

   require 'app/start.php';

//    echo "This is Homebrew";

   if(isset($_FILES['file'])){

    // $time     = md5($_FILES["document"]['name'] . microtime());
    // $fileInfo = pathinfo($_FILES["document"]["name"]);
    // $doc_name = $time . '.' . $fileInfo['extension'];
    // move_uploaded_file($_FILES["document"]["tmp_name"], "./uploads/student_documents/online_admission_doc/" . $doc_name);
    // $data['document'] = $doc_name;
    // $data_img         = array('id' => $insert_id, 'document' => $doc_name);
    // $this->onlinestudent_model->edit($data_img); 

    $file = $_FILES['file'];

    // File details
    $name = $file['name'];
    $tmp_name = $file['tmp_name'];

    $extension = explode('.', $name);
    $extension = strtolower(end($extension)); 

    // Temp Detail
    $key = md5(uniqid());
    $tmp_file_name = "{$key}.{$extension}";
    $tmp_file_path = "files/{$tmp_file_name}";

    var_dump($tmp_name);
    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"

        ]);
        // Remove the file
        // unlink($tmp_file_path);

    } catch(S3Exception $e){

        die("There was an error uploading the file!");

    }
   }

?>

<html>
    <head>
        <title>
            AWS S3  UPLOAD
        </title>
    </head>
    <body>
        <form action="" method="POST" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" name="submit" value="Upload">
        </form>
    </body>
</html>