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.
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.
Steps3: Create Image File Upload Form
Now in index.php, we will create image upload form to upload image files.
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.
You can view the live demo from the Demo link and can download the script from the Download link below.
Demo [sociallocker]Download[/sociallocker]
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]
This is perfect! I found it very valuable thank you so much!
BalasHapusHelp me upload to 1 folder
BalasHapusthanks
Matt,
BalasHapusThanks!
Ok, send your code with details so that I can help you.
BalasHapusI have used this code but it stopped working and i can see it doesn't seem to work on your demo also if there is any other simple way i can do the same that would be great
BalasHapusThanks
RPS Rathore
May be your trial period expired , so may be you need valid AWS Access keys and secret keys.You can contact AWS support for that. Thanks!
BalasHapusPosting Komentar