title: Uploading large files to Amazon S3
body: <p>I've manage to get the following script to work with smaller files. But when I try to upload files around 10MB or more, it says that it is completed but the file does not show up in my S3 bucket.</p>
  
  <p>Any ideas why it will upload smaller files and not file 10MB or greater?</p>
  
  <pre><code>&lt;?php
  //System path for our website folder
  define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
  //URL for our website
  define('WEBROOT', htmlentities(
      substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")),
      ENT_QUOTES
  ));
  
  //Which bucket are we placing our files into
  $bucket = 'bucket.mysite.com';
  // This will place uploads into the '20100920-234138' folder in the $bucket bucket
  $folder = date('Ymd-His').'/'; //Include trailing /
  
  //Include required S3 functions
  require_once DOCROOT."includes/s3.php";
  
  //Generate policy and signature
  list($policy, $signature) = S3::get_policy_and_signature(array(
      'bucket'        =&gt; $bucket,
      'folder'        =&gt; $folder,
      ));
  ?&gt;
  
  
  &lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $("#file_upload").uploadify({
              'uploader'      : '&lt;?= WEBROOT ?&gt;files/uploadify/uploadify.swf',
              'buttonText'        : 'Browse',
              'cancelImg'     : '&lt;?= WEBROOT ?&gt;files/uploadify/cancel.png',
              'script'        : 'http://s3.amazonaws.com/&lt;?= $bucket ?&gt;',
              'scriptAccess'      : 'always',
              'method'        : 'post',
              'scriptData'        : {
                  "AWSAccessKeyId"        : "&lt;?= S3::$AWS_ACCESS_KEY ?&gt;",
                  "key"               : "${filename}",
                  "acl"               : "authenticated-read",
                  "policy"            : "&lt;?= $policy ?&gt;",
                  "signature"         : "&lt;?= $signature ?&gt;",
                  "success_action_status"     : "201",
                  "key"               : encodeURIComponent(encodeURIComponent("&lt;?= $folder ?&gt;${filename}")),
                  "fileext"           : encodeURIComponent(encodeURIComponent("")),
                  "Filename"          : encodeURIComponent(encodeURIComponent(""))
              },
              'fileExt'       : '*.*',
              'fileDataName'      : 'file',
              'simUploadLimit'    : 2,
              'multi'         : true,
              'auto'          : true,
              'onError'       : function(errorObj, q, f, err) { console.log(err); },
              'onComplete'        : function(event, ID, file, response, data) { console.log(file); }
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <hr>
  
  <pre><code> &lt;?php
      class S3 {
          public static $AWS_ACCESS_KEY       = '&lt; Your access key &gt;';
          public static $AWS_SECRET_ACCESS_KEY    = '&lt; Your secrete key &gt;';
  
  
      /*
       * Purpose:
       *      Actionscript encodes '+' characters in the signature incorrectly - it makes
       *      them a space instead of %2B the way PHP does. This causes uploadify to error
       *      out on upload. This function recursively generates a new policy and signature
       *      until a signature without a + character is created.
       * Accepts: array $data
       * Returns: policy and signature
       */
      public static function get_policy_and_signature( array $data )
      {
          $policy = self::get_policy_doc( $data );
          $signature = self::get_signature( $policy );
  
          if ( strpos($signature, '+') !== FALSE )
          {
              $data['timestamp'] = intval(@$data['timestamp']) + 1;
              return self::get_policy_and_signature( $data );
          }
  
          return array($policy, $signature);
      }
  
      public static function get_policy_doc(array $data)
      {
          return base64_encode(
              '{'.
                  '"expiration": "'.gmdate('Y-m-d\TH:i:s\Z', time()+60*60*24+intval(@$data['timestamp'])).'",'.
                  '"conditions": '.
                  '['.
                      '{"bucket": "'.$data['bucket'].'"},'.
                      '["starts-with", "$key", ""],'.
                      '{"acl": "authenticated-read"},'.
                      //'{"success_action_redirect": "'.$SWFSuccess_Redirect.'"},'.
                      '{"success_action_status": "201"},'.
                      '["starts-with","$key","'.str_replace('/', '\/', $data['folder'] ).'"],'.
                      '["starts-with","$Filename",""],'.
                      '["starts-with","$folder",""],'.
                      '["starts-with","$fileext",""],'.
                      '["content-length-range",0,5242880]'.
                  ']'.
              '}'
          );
      }
  
      public static function get_signature( $policy_doc ) {
          return base64_encode(hash_hmac(
              'sha1', $policy_doc, self::$AWS_SECRET_ACCESS_KEY, true
          ));
      }
      }
  </code></pre>