使用AWS PHP SDK将图像保存到S3

时间:2017-07-11 08:07:20

标签: php amazon-web-services amazon-s3 aws-sdk

我正在尝试将图像上传到S3,但是图像上传但是大小为0字节,这与源文件不同。不知道我做错了什么,请有人指导我正确的方向。

upload.php在

之下
<!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;
 }

输出:

图片已上传

该文件在S3上可用,但大小为0.我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要将实际文件作为参数添加到putObject方法中。

试试这个;

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

您可以查看AWS文档上的示例。 http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#uploading-a-file

答案 1 :(得分:0)

代码中的错字:

reader

应该是:

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