Reputation: 185
I am trying to upload photos to my S3 bucket. The file being uploaded is making it to my S3 bucket successfully. The problem? Yup, it is in the form of 'application/octet-stream' though. Given the implementation below, what changes do I need to make and where?
index.php
<?php
require 's3.php';
$s3 = new S3('AWS_ACCESS_KEY_ID','AWS_SECRET KEY', 'region: us-west-2');
if(isset($_FILES['file'])){
$file = $_FILES['file'];
$name = $file['name'];
$tmp_name = $file['tmp_name'];
var_dump($tmp_name);
$extension = explode('.', $name);
$extension = strtolower(end($extension));
// var_dump($extension);
$key = md5(uniqid());
$tmp_file_name = "{$key}.{$extension}";
$tmp_file_path = "files/{$tmp_file_name}";
move_uploaded_file($tmp_name, $tmp_file_path);
try{
S3::putObject(
$tmp_file_path,
'mazzo-php-app',
$tmp_file_name,
S3::ACL_PUBLIC_READ,
array(),
array()
);
unlink($tmp_file_path);
} catch(S3Exception $e){
die("There was an error uploading the file.");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload</title>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
s3.php
// Content-Type
if (!isset($input['type']))
{
if (isset($requestHeaders['Content-Type']))
$input['type'] =& $requestHeaders['Content-Type'];
elseif (isset($input['file']))
$input['type'] = self::__getMIMEType($input['file']);
else
$input['type'] = 'application/octet-stream';
}
Upvotes: 2
Views: 1393
Reputation: 269550
The putObject()
documentation shows a ContentType
field:
$result = $client->putObject([
'ACL' => 'private|public-read|public-read-write|authenticated-read|aws-exec-read|bucket-owner-read|bucket-owner-full-control',
'Body' => <string || resource || Psr\Http\Message\StreamInterface>,
'Bucket' => '<string>', // REQUIRED
'ContentType' => '<string>',
...
]);
ContentType: A standard MIME type describing the format of the contents.
Upvotes: 1