PHP Direct Upload to S3

Here’s an easy way to upload directly to S3 from an HTML form. No Java, no Flash. Thanks to @RaamDev for his handy little bit of code for doing HMAC signatures. Based on the documentation from Amazon. Makes use of the AWS SDK for PHP to list the buckets.

Screen shots:

Bucket Selection

File Selection

File Selected

Upload Success


<TITLE>Upload a file to S3</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
<H1>S3 File Upload</H1>


require_once 'AWSSDKforPHP/sdk.class.php';
if (!class_exists('CFRuntime')) die('No direct access allowed.');
        $name => array(
                'key' => $accesskey,
                'secret' => $secretkey,
                'certificate_authority' => false
        '@default' => $name

$s3 = new AmazonS3();

if(!isset($_GET['bucket'])) {
        echo "<P>Please select a bucket:</P>\n";
        echo '<FORM ACTION="#">';
        foreach ($buckets as $b) { echo '<INPUT TYPE="radio" name="bucket" value="'.$b.'">'.$b."</INPUT><BR>\n"; }
        echo '<BR><INPUT TYPE="submit" VALUE="Use this bucket">'."\n";

        } else {
        $bucket = $_GET['bucket'];

{"expiration": "2012-01-31T00:00:00Z",
  "conditions": [
    {"bucket": "'.$bucket.'"},
    ["starts-with", "$key", ""],
    {"acl": "public-read"},
    {"success_action_redirect": "success.php"},
    ["starts-with", "$Content-Type", ""]

 * Calculate HMAC-SHA1 according to RFC2104
 * See
function hmacsha1($key,$data) {
    if (strlen($key)>$blocksize)
        $key=pack('H*', $hashfunc($key));
    $hmac = pack(

    return bin2hex($hmac);

 * Used to encode a field for Amazon Auth
 * (taken from the Amazon S3 PHP example library)
function hex2b64($str)
    $raw = '';
    for ($i=0; $i < strlen($str); $i+=2)
        $raw .= chr(hexdec(substr($str, $i, 2)));
    return base64_encode($raw);

/* Create the Amazon S3 Policy that needs to be signed */

 * Base64 encode the Policy Document and then
 * create HMAC SHA-1 signature of the base64 encoded policy
 * using the secret key. Finally, encode it for Amazon Authentication.
$base64_policy = base64_encode($policy);
$signature = hex2b64(hmacsha1($secretkey, $base64_policy));

    <form action="https://<?=$bucket?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="key" value="${filename}">
      <input type="hidden" name="AWSAccessKeyId" value="<?=$accesskey;?>">
      <input type="hidden" name="acl" value="public-read">
      <input type="hidden" name="success_action_redirect" value="">
      <input type="hidden" name="policy" value="<?=$base64_policy?>">
      <input type="hidden" name="signature" value="<?=$signature?>">
      <input type="hidden" name="Content-Type" value="image/jpeg">
      <!-- Include any additional input fields here -->

      <input name="file" type="file">
      <input type="submit" value="Upload File to S3 bucket <?=$bucket?>">
        <P><A HREF=".">Use a different bucket</A></P>


<TITLE>Upload Success!</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
<H1>Upload Success!</H1>


echo "<P>Successfully uploaded ".$_GET['key']." to bucket ".$_GET['bucket'].".</P>\n";
$url = "https://".$_GET['bucket']."".$_GET['key'];
echo '<P>Direct URL to the file is <A HREF="'.$url.'">'.$url."</A></P>\n";

include '../footer.php';

17 Comments On “PHP Direct Upload to S3”

  1. i have this error Uncaught exception ‘CFCredentials_Exception’ with message ‘If more than one credential set is provided, a default credential set (identified by the key “@default”) must be specified


  2.  Enjoyed the post. Two questions for you. 1) In AWSSDKforPHP_sampleslib there is a progress_bar sample. Any idea how to incorporate it into the script? 2) Any guess as to how large a file the script will handle?


  3. What do I need to do to copy into a folder of a bucket?
    What changes in code needs to be done?


  4. another question is where does the value for ${filename} come from?


  5. How do I do to check if the file already exists on s3. If yes, opt the user to overwrite?


  6. Can you think of a way to limit the file size that’s selected?


  7. Is there any chance of this being updated, with the release of Amazon PHP SDK 2?


  8. i tried to use this code 

    i got this error..

    Fatal error: Uncaught exception ‘CFCredentials_Exception’ with message ‘No credentials were provided. The SDK attempts to retrieve Instance Profile credentials from the EC2 Instance Metadata Service, but doing this requires the “default_cache_config” option to be set in the file or constructor. In order to cache the retrieved credentials.’ in /var/www/s3upload/sdk/sdk.class.php on line 439


  9. What is AWSSDKforPHP/sdk.class.php file here. I downdlod the AWS S3 SDK But not finding this type of file.


Leave a Reply

Your email address will not be published. Required fields are marked *