下载脚本here包含4个文件
- S3.php // Amazon S3的库文件
- s3_config.php //重点配置文件
- image_check.php //文件校验
- 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 该文件包含PHP和HTML代码提交表单。
<?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>
创建一个桶,然后右击选择属性,添加许可选择Everyone使检查列表框中。
image_check.php 此文件帮助您获取文件扩展名。
<?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");
?>
检查你的php'post_max_size'和'upload_max_filesize' ini设置。我认为它被限制在4MB以下。 – Pieter
我的网站托管在亚马逊aws,我不知道如何做到这一点.. – Diasline
创建一个php文件,内容为<?php phpinfo();'。将其上传到您的aws服务器并在浏览器中打开该网址。它会显示你所有的PHP设置。 – Pieter