Amazon S3 File Upload using PHP

If your website has too many image files and looking for Amazon S3 to store images there to reduces file load time and bandwidth usage, then you're here at right place. Amazon Simple Storage Service (Amazon S3) is a popular web service that provides highly scalable, durable and secure storage. It's very easy to use and makes use of bucket to store and retrieve data from anywhere and anytime on the web. So here in this tutorial you will learn how to upload files to Amazon S3 using PHP. The tutorial explained in very easy steps with live demo and link to download source code.

So let’s start the coding. We will have following file structure for the demo to upload files to Amazon S3 using PHP.

  • index.php

  • functions.php

  • config.php

  • s3.php

Steps1: Create Amazon S3 Account
First we need to create Amazon S3 account and get your bucket name and access keys to use for uploading files.

Steps2: Configure S3 Details
After getting Amazone S3 account details, we will define Amazon S3 account details in config.php with bucket name, access key and secret key. We will also include amazon s3 library file s3.php to handle file upload S3 server.

// Bucket Name
//AWS access info
$awsAccessKey = 'Access Key';
$awsSecretKey = 'Security Key';
//instantiate the class
$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);

Steps3: Create Image File Upload Form
Now in index.php, we will create image upload form to upload image files.

<div class="container">
<h2>Amazon S3 File Upload using PHP</h2>
<form action="" method='post' enctype="multipart/form-data">
<h3>Upload Image</h3><br/>
<input type='file' name='upload_file'/>
<input type='submit' name="upload_files" value='Upload'/>
if($file_upload_message) {
echo $file_upload_message;

Steps4: Handle Amazon S3 File Upload

Now finally we will handle image file upload to Amazon S3 server on form submit. We will include functions.php to use function getFileExtension() to get file extension to check for supported image file types. We will also include s3.php to use S3 object to upload file using S3 function putObjectFile() and display file upload message.

$file_upload_message = '';
if(isset($_POST["upload_files"])) {
$file_name = $_FILES['upload_file']['name'];
$file_size = $_FILES['upload_file']['size'];
$tmp_file = $_FILES['upload_file']['tmp_name'];
$valid_file_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
$file_extension = getFileExtension($file_name);
if($file_name) {
if(in_array($file_extension,$valid_file_formats)) {
if($file_size < (1024*1024)) {
$new_image_name = time().".".$file_extension;
if($s3->putObjectFile($tmp_file, $bucket , $new_image_name, S3::ACL_PUBLIC_READ)) {
$file_upload_message = "File Uploaded Successfully to amazon S3.<br><br>";
$file_upload_message .= '<b>Upload File URL:</b>'.$uploaded_file_path."<br/>";
$file_upload_message .= "<img src='$uploaded_file_path'/>";
} else {
$file_upload_message = "<br>File upload to amazon s3 failed!. Please try again.";
} else {
$file_upload_message = "<br>Maximum allowed image upload size is 1 MB.";
} else {
$file_upload_message = "<br>This file format is not allowed, please upload only image file.";
} else {
$file_upload_message = "<br>Please select image file to upload.";

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo [sociallocker]Download[/sociallocker]

You may like these posts