Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created November 23, 2012 22:38
Show Gist options
  • Save martinbean/4137600 to your computer and use it in GitHub Desktop.
Save martinbean/4137600 to your computer and use it in GitHub Desktop.
Code samples for “Uploading files with Amazon’s new PHP SDK” blog post.
{
"require": {
"aws/aws-php-sdk": "dev-master"
}
}
<?php
$attributes = '';
foreach ($formAttributes as $key => $value) {
$attributes.= sprintf(' %s="%s"', $key, $value);
}
?>
<form <?php echo $attributes; ?>>
<fieldset>
<?php foreach ($formInputs as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />
<?php endforeach; ?>
<input type="submit" name="upload" value="Upload" />
</fieldset>
</form>
<?php
$formAction = $formAttributes['action'];
?>
<form action="<?php echo $formAction; ?>" method="post">
<fieldset>
<input type="file" name="image" id="image" accept="image/jpeg">
</fieldset>
</form>
<script>
$('#image').uploadify({
formData: <?php echo json_encode($formInputs); ?>,
fileObjName: 'file',
fileTypeExts: '*.jpg',
onUploadError: function(file, errorCode, errorMsg, errorString) {
/*
* logic to display error message
*/
},
onUploadSuccess: function(file, data, response) {
/*
* logic to display success message
*/
},
swf: '/assets/swf/uploadify.swf',
uploader: '<?php echo $formAttributes['action']; ?>'
});
</script>
<?php
use Aws\Common\Enum\Region;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception;
use Aws\S3\Model\PostObject;
use Aws\S3\S3Client;
$s3 = S3Client::factory(array(
'key' => 'Your Amazon S3 access key',
'secret' => 'Your Amazon S3 access secret',
'region' => Region::EU_WEST_1
));
$bucket = 'Your bucket name';
$options = array(
'acl' => CannedAcl::PUBLIC_READ,
'Content-Type' => 'image/jpeg',
'key' => 'your-filename.jpg',
'success_action_status' => 201,
'filename' => '^'
);
$postObject = new PostObject($s3, $bucket, $options);
$postObject->prepareData();
$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment