我正在尝试用 php 创建一个表单来将文件上传到 S3,我的页面如下:
默认.php:
<form method="post" class="form-group" action="" enctype="multipart/form-data">
<label>Upload file</label>
<input type="file" name="fileToUpload" id="fileToUpload" class="form-control">
<input type="submit" name="submit" value="Submit-App" class="btn btn-primary">
</form>
数据库更新.php:
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
if (isset($_POST['submit'])) {
$bucketName = '*********';
$IAM_KEY = '********************';
$IAM_SECRET = '********************************';
try {
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => $IAM_KEY,
'secret' => $IAM_SECRET
),
'version' => 'latest',
'region' => 'eu-west-1'
)
);
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
$keyName = 'testfile/' . basename($_FILES["fileToUpload"]['name']);
try {
// Uploaded:
$file = $_FILES["fileToUpload"]['tmp_name'];
$s3->putObject(
array(
'Bucket'=>$bucketName,
'Key' => $keyName,
'SourceFile' => $file,
'StorageClass' => 'REDUCED_REDUNDANCY'
)
);
} catch (S3Exception $e) {
die('Error:' . $e->getMessage());
} catch (Exception $e) {
die('Error:' . $e->getMessage());
}
}
出于某种原因,这不起作用,它会testfile/在目标存储桶内创建文件夹,但不会上传文件。我试着用action="dbupdate.php"和去除器制作一个正常的表格if(isset($_POST['submit'])),它工作得很好,但我需要它是另一种方式(没有action文件)
有什么建议么?谢谢
MMMHUHU