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.
<?php
// Bucket Name
$bucket="phpzag_demo";
require_once('S3.php');
//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>
<br>
<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'/>
</form>
<?php
if($file_upload_message) {
echo $file_upload_message;
}
?>
</div>
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.
<?php
include('functions.php');
$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)) {
include('config.php');
$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>";
$uploaded_file_path='http://'.$bucket.'.s3.amazonaws.com/'.$new_image_name;
$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]