9lessons programming blog
Loading Search
Wall Script
Tuesday, August 7, 2012

Upload Files to Amazon S3 using PHP

Are you looking for Amazon S3 bucket file upload from your web project using PHP technology., if yes take a quick look at this post demo. Amazon S3 is the best option to reduce the bandwidth cost as well file load time. Upload functionality is the most sensitive part in web project, one small mistake hackers will upload miscellaneous files. If you are connect with Amazon S3 you will be safe side.

Upload Files to Amazon S3 PHP

Download Script     Live Demo

Download script contains four files.
S3.php //Amazon S3 library file
s3_config.php //Key configuration file
image_check.php //File validator
index.php

Login to Amazon account and click here to get Access Credentials.
Upload Files to Amazon S3 PHP

s3_config.php
Here you have to provide amazon key details.
<?php
// Bucket Name
$bucket="BucketName";
if (!class_exists('S3'))require_once('S3.php');

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY');
if (!defined('awsSecretKey')) define('awsSecretKey', 'ACCESS_Secret_KEY');

$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
?>

index.php
The file contains PHP and HTML code to submit form.
<?php
include('image_check.php'); // getExtension Method
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);

if(strlen($name) > 0)
{
// File format validation
if(in_array($ext,$valid_formats))
{
// File size validation
if($size<(1024*1024))
{
include('s3_config.php');
//Rename image name.
$actual_image_name = time().".".$ext;

if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$msg = "S3 Upload Successful.";
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
echo "<img src='$s3file'/>";
echo 'S3 File URL:'.$s3file;
}
else
$msg = "S3 Upload Fail.";

}
else
$msg = "Image size Max 1 MB";

}
else
$msg = "Invalid file, please upload image file.";

}
else
$msg = "Please select image file.";

}
?>

//HTML Code
<form action="" method='post' enctype="multipart/form-data">
Upload image file here
<input type='file' name='file'/> <input type='submit' value='Upload Image'/>
<?php echo $msg; ?>
</form>

Create a bucket and right click select properties, add permission select Everyone enable check list box.
Upload Files to Amazon S3 PHP

image_check.php
This file help you to get file extension.
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//Here you can add valid file extensions. 
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
?>

Share this post

Comments
{ 35 comments }
Bharat said...

Yet another useful tutorial, I'm looking for multiple file uploader, will that be possible ?

mahesh verma said...

Awesome work thanks for sharing

Arslan Qamar said...

Can you share same like script for ebay?????

Jagan Krishnaraj said...

Nice Work. Selecting multiple files at once?

Blogger said...

Can i upload to a folder

brad said...

awesome awesome aswesome

Mark said...

Very good tutorial Srinivas!

Anonymous said...

Any way to do this with media files?

Sai said...

How would you tweek this so you can upload files that aren't just basic image files, like PDFs, or Zip files?
Thank you for any help....

phphunger said...

Superb tutorial...i liked it..

dependable said...

I would like to upload mp3 files. I changed the code, but every time I try to upload mp3 file, script returns 'S3 Upload Fail'

مدير الإدارة said...

amazing tutorial, i just want to add this
for Creating a bucket follow the link https://console.aws.amazon.com/s3/home

thanks

Anonymous said...

showing file upload progress would make this tutorial perfect

Anton said...

Thanks for you good post ;)

siddhu vydyas said...

how to create a bucket plz give reply

Anonymous said...

@siddhu vydyas you can use a firefox extension called s3 organizer. and you can create a bucket.

Krishoo said...

Nice I like it. Thank You..

missroyaldisaster said...

Great job done here, many thanks for this code, I had no idea this is possible at all. And I also would like to say that you have a very informative blog in general, keep it up this way!

Kawaljeet singh said...

Thanks for the code. Can i upload any video and audio files with mp3,mp4 extensions and if i can do can u please let me know where the code has to be changed and what has to be changed ?

hari said...

can we upload a video file into a folder using kohana frame work in php

riza said...

It's possible to add progress bar ?. I'm stuck with this. Thanks :-)

Anonymous said...

What should be the setting if one is not using the default us data center?

varsha said...

Thanks nice post

Narsimha said...

Thanks,very nice post. Append folder name to $actual_image_name then it will put the file to FOLDER.

Anonymous said...

Will this be a direct upload to the server, or moving it from temp directory to the server?

Anonymous said...

Great Job

Biswanath Bose said...

Great job, my only work is to rename the folder

Biswanath Bose said...

Will this be a direct upload to the server, or moving it from temp directory to the server?

Biswanath Bose said...

In my bucket I have a folder, I want to upload the file to the specific folder within my bucket, how can I do that?

Unknown said...

In shared hosts running Apache + PHP, the maximum file size to upload will be fixed by 'post_max_size' or this rule will be bypassed?



Anonymous said...

"In shared hosts running Apache + PHP, the maximum file size to upload will be fixed by 'post_max_size' or this rule will be bypassed?"

Looks like s3 is not even called for until the file size is checked. This means you should be limited to the php.ini max size. Fill me in if I'm wrong.

Alex Medeiros said...

Very Nice Tutorial,

I am using this exemplo to upload my images.

I have only a littler question:

I am try to send to S3 some resize images. I send the original photo and after I need to resize this image and send to S3 the resized images.

How can I resize a image and send to S3?

Anonymous said...

nice one....

Evgeniy Doog said...

Good night Srinivas Tamada. You have a great article and very informative website. But with this script, I have a problem. The fact is that I have no way to change the time zone on the server. what to do in such cases, how to deal with the problem?. I will be glad to hear what you think about it.

P.S. Sorry for my bad English

Suraj Sahoo said...

Very Nice!!!

Thanks for Shearing...

Post a Comment