简体   繁体   English

使用php将文件上传到s3

[英]Upload file to s3 with php

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 : 我使用php在s3中上传文件,但是我的代码可以正常工作,但是当文件选择上传时非常繁琐,例如4 MB操作失败,并且出现以下错误:

NoSuchKey The specified key does not exist.1469625998.pdf136D99DB27E5B57FE6UXAbj45afkoAlfysDm2+EPhBqHOhwc64gaG1w/b1AXAsfhnF7qwlankwuflh+ZO1sf/JBe6tA= NoSuchKey指定的密钥不存在.1469625998.pdf136D99DB27E5B57FE6UXAbj45afkoAlfysDm2 + EPhBqHOhwc64gaG1w / b1AXAsfhnF7qwlankwuflh + ZO1sf / JBe6tA =

But when file is about 1.3 MB, upload success. 但是,当文件约为1.3 MB时,上传成功。 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)) ! 我想知道为什么不能上传任何大小的文件,因为我没有指定任何大小的文件,例如if($ size <(1024 * 1024))

Download script here which contain 4 files 在这里下载脚本其中包含4个文件

  • S3.php //Amazon S3 library file S3.php // Amazon S3库文件
  • s3_config.php //Key configuration file s3_config.php //密钥配置文件
  • image_check.php //File validator image_check.php //文件验证器
  • index.php index.php

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM