用php上传文件到s3

I use php to upload file in s3, my codes work properly however when file selects to upload is very heavy like 4 MB operation fails and i have this error :

NoSuchKeyThe specified key does not exist.1469625998.pdf136D99DB27E5B57FE6UXAbj45afkoAlfysDm2+EPhBqHOhwc64gaG1w/b1AXAsfhnF7qwlankwuflh+ZO1sf/JBe6tA=

But when file is about 1.3 MB, upload success. below is my codes :

$name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
 include('s3folder/s3_config.php');
 $s3->putObjectFile($tmp, $bucket , $name, S3::ACL_PUBLIC_READ);

I'm wondering why any size of file can't be uploaded since i don't specify any size file like if($size<(1024*1024)) !

Download script here which contain 4 files

  • S3.php //Amazon S3 library file
  • s3_config.php //Key configuration file
  • image_check.php //File validator
  • index.php

s3_config.php

<?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.

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