Mahesh inCV
Mahesh inCV

Reputation: 101

Saving image to S3 using AWS PHP SDK

I am trying to upload an image to S3, but the image is getting uploaded but the size is 0 bytes which is different from the source file. Not sure what i am doing wrong, can anyone direct me on the right direction please.

upload.php is below

<!DOCTYPE html>
<html>
<body>

<form action="save.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

save.php

<?php
include 'connecttoaws.php'

$bucket = 'xyxdskjflsdafjsakf';
$file = $FILES["img"]["name"];

if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img"]["tmp_name"]);
if($check !== false) {

$uploadOK = 1;
$result = $client->putObject(array(
'Bucket' => $bucket,
'ContentType' => 'image/png',
'Key' => $file
 ));

 echo 'Image Uploaded';

 }
 else{
 echo "File not uploaded";
 $uploadOK = 0;
 }

output:

Image uploaded

the file is available on S3 but the size is 0. What am i doing wrong?

Upvotes: 2

Views: 2802

Answers (2)

Dileep Kumar
Dileep Kumar

Reputation: 1107

Typo in your code:

$file = $FILES["img"]["name"];

should be:

$file = $_FILES["img"]["name"];

Upvotes: 0

Memduh
Memduh

Reputation: 866

You need to add the actual file as a parameter to putObject method.

Try this;

$result = $client->putObject(array(
'Bucket' => $bucket,
'ContentType' => 'image/png',
'SourceFile' => $file,
'Key' => $file
));

You can check the example on AWS docs. http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#uploading-a-file

Upvotes: 4

Related Questions