views:

330

answers:

2

Hi, I need to upload a given image using Amazon S3

I have this PHP:

<?
$uploaddir = 'images/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "Data Uploaded Successfully";
} else {
    echo "ERROR";
}
?>

but it gives me this error:

<?xml version="1.0" encoding="UTF-8" ?>
<Error>
    <Code>MethodNotAllowe</Code>
    <Message>The specified method is not allowed against this resource.</Message>
    <ResourceType>OBJECT</ResourceType>
    <Method>POST</Method>
    ....
    <AllowedMethod>PUT</AllowedMethod>
    ....
</Error>

How can I upload a file?

+4  A: 

You are using the POST method (which is PHP defaults) to submit the data. Most webapplications differenciate between the verbs GET, PUT and POST (see W3 RFC on Verbs).

S3 wants you to use <AllowedMethod>PUT</AllowedMethod> as the method. move_uploaded_file isn't able to do that. Before you start writing code for doing PUT requests, maybe you should take a look at some PHP S3 libs.

Marcel J.
+3  A: 

Have a try at Zend Framework, there is a great class (Zend_Service_Amazon_S3) to handle all your S3 hassle.

http://framework.zend.com

require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->createBucket("my-own-bucket");
$s3->putObject("my-own-bucket/myobject", $file);
funktioneer