I want to give direct upload feature to my users. I mean instead of upload from their pc, they also should upload an image from an external source (full url to img will be my source) to my Amazon S3 bucket.
But my service is really heavy. If I save image to my VPS, before S3 upload, this will kill my VPS server. Can I do direct upload without saving the file before upload to S3.
Can I check size, extension etc on remote source?
Currently with this script they only upload with an upload form.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//Here you can add valid file extensions.
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
print_r($_FILES);
$ext = getExtension($name);
if(strlen($name) > 0) {
// File format validation
if(in_array($ext,$valid_formats)) {
// File size validation
if($size<(1024*1024)) {
include('amazons3/s3_config.php');
//Rename image name.
$actual_image_name = time().".".$ext;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) ) {
$msg = "S3 Upload Successful.";
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
echo "<img src='$s3file'/>";
echo 'S3 File URL:'.$s3file;
}
else {
$msg = "S3 Upload Fail.";
}
}
else {
$msg = "Image size Max 1 MB";
}
}
else {
$msg = "Invalid file, please upload image file.";
}
}
else {
$msg = "Please select image file.";
}
}
What should I do to implement from URL upload?
How can I control the image for extension, size etc like my current validations?