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.

Download Script
Live DemoDownload script contains four files.
S3.php //Amazon S3 library file
s3_config.php //Key configuration file
image_check.php //File validator
index.php
s3_config.php //Key configuration file
image_check.php //File validator
index.php
Login to Amazon account and click here to get Access Credentials.

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);
?>
// 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>
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.

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");
?>
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");
?>










Yet another useful tutorial, I'm looking for multiple file uploader, will that be possible ?
Awesome work thanks for sharing
Can you share same like script for ebay?????
Nice Work. Selecting multiple files at once?
Can i upload to a folder
awesome awesome aswesome
Very good tutorial Srinivas!
Any way to do this with media files?
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....
Superb tutorial...i liked it..
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'
amazing tutorial, i just want to add this
for Creating a bucket follow the link https://console.aws.amazon.com/s3/home
thanks
showing file upload progress would make this tutorial perfect
Thanks for you good post ;)
how to create a bucket plz give reply
@siddhu vydyas you can use a firefox extension called s3 organizer. and you can create a bucket.
Nice I like it. Thank You..
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!
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 ?
can we upload a video file into a folder using kohana frame work in php
It's possible to add progress bar ?. I'm stuck with this. Thanks :-)
What should be the setting if one is not using the default us data center?
Thanks nice post
Thanks,very nice post. Append folder name to $actual_image_name then it will put the file to FOLDER.
Will this be a direct upload to the server, or moving it from temp directory to the server?
Great Job
Great job, my only work is to rename the folder
Will this be a direct upload to the server, or moving it from temp directory to the server?
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?
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?
"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.
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?
nice one....
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
Very Nice!!!
Thanks for Shearing...