0

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?

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
hakki
  • 6,181
  • 6
  • 62
  • 106
  • 1
    You can check nothing without downloading the content. URL is just a string. What's the problem to download image to the server, validate it, upload to S3, then delete from the server? If such a basic operation kills the server, it's time to change the server. – Alex Blex Dec 21 '16 at 10:35
  • @AlexBlex Actually I can with cURL : http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file – hakki Dec 21 '16 at 10:36
  • Fair point, but you will need to rely on MIME type of the header, which might not be in line with actual binary. Not saying about file extension, which makes even less sense with remote resources. – Alex Blex Dec 21 '16 at 10:58
  • Is [this](http://stackoverflow.com/a/7595984/1695906) what you're asking about? Requesting S3 to fetch the file directly from the remote resource? That isn't supported. – Michael - sqlbot Dec 21 '16 at 12:52

0 Answers0