File: //var/www/santabrigade/awsS3/upload_function.php
<?php
include_once '/var/www/santabrigade/wp-config.php';
require_once 'autoload.php';
use Aws\S3\S3Client;
/* -----------------upload function for image-------------- */
function uploadToS3($usrId, $key, $file_name, $temp_file_location, $mimeType){
$credentials = new Aws\Credentials\Credentials("AKIATU377FQG7XAY5H4E" , "pFWwubHkflbcJiCiM+n+s78wqJCofN8HMxZ2Ir5h");
$s3 = new S3Client([
'region' => 'ap-south-1',
'version' => 'latest',
'credentials' => $credentials,
]);
$result = $s3->putObject([
'Bucket' => 'santabrigade',
'Key' => $usrId.$key.$file_name,
'SourceFile' => $temp_file_location,
'ACL' => 'public-read',
'ContentType' => $mimeType,
]);
return $result['@metadata']['statusCode'];
}
/* -----------------delete function for image-------------- */
function deleteToS3($usrId, $key, $file_name){
$credentials = new Aws\Credentials\Credentials("AKIATU377FQG7XAY5H4E" , "pFWwubHkflbcJiCiM+n+s78wqJCofN8HMxZ2Ir5h");
$s3 = new S3Client([
'region' => 'ap-south-1',
'version' => 'latest',
'credentials' => $credentials,
]);
$result = $s3->deleteObject([
'Bucket' => 'santabrigade',
'Key' => $usrId.$key.$file_name,
]);
return $result['@metadata']['statusCode'];
}
/* -----------------get extension-------------- */
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
/* -----------------get mimetype-------------- */
function getMimeType($file)
{
// MIME types array
$mimeTypes = array(
"ai" => "application/postscript",
"cdr" => "image/x-cdr",
"png" => "image/png",
"PNG" => "image/png",
"jpeg" => "image/jpeg",
"JPEG" => "image/jpeg",
"jpg" => "image/jpeg",
"JPG" => "image/jpeg",
"psd" => "image/photoshop",
"pdf" => "application/pdf",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"txt" => "text/plain"
);
$extension = end(explode('.', $file));
return $mimeTypes[$extension]; // return the array value
}
?>