0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.118.144.50


Current Path : /var/www/html/univadmin/
Upload File :
Current File : /var/www/html/univadmin/upload-message-attachments.php

<?php

require_once(__DIR__."/../aws/aws-autoloader.php");
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
header("Access-Control-Allow-Origin: *");
header('Content-Type: multipart/form-data');
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, x-auth-origin, x-auth-token, x-auth-type");
$ds = DIRECTORY_SEPARATOR;
$date = date("dmYhis");
$file_resp = "";

if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 
    die();
}
// var_dump($_FILES);
$user = json_decode($_REQUEST['body'],true);
// var_dump($user);
// die();
$univcode = $user['fcuruniv'];

if (0 < $_FILES['filefield']['error']) {
    http_response_code(409);
    echo 'Error: ' . $_FILES['filefield']['error'] . '<br>';
    $file_resp = "Failed to upload file!";
} else {
    // $name = $date . '_' . $_FILES['file']['name'];
    $name = $_FILES['filefield']['name'];

    $theCount = substr_count($name, '.') - 1; // count how many time the period occurs in the string
   
    $file_name = str_replace($_FILES['filefield']['name'], $name, $_FILES['filefield']['name']); 
	$path = realpath($_FILES['filefield']['tmp_name']);
    $s3 = S3Client::factory(
		array(
			'credentials' => array(
				'key' => IAM_KEY,
				'secret' => IAM_SECRET
			),
			'version' => "latest",
			'region'  => 'ap-south-1'
		)
    );
    

    $S3filepath = $univcode.'/'.date('Y-m-d-H:i:s', time()).'-'.$file_name;
    $result = $s3->putObject([
        'Bucket' => 'university-group-attachment', 
        'Key' => $S3filepath, 
        'SourceFile' => $path, 
        'ContentType' => $_FILES['filefield']['name']
    ]);

    if($result){
        echo $S3filepath;
    } else{
        http_response_code(409);
        echo "Upload Failed";
    }

}
?>