1 回答
TA贡献1834条经验 获得超8个赞
添加条件以检查文件是否存在
<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
if (isset($_POST['submit']) && !empty($_FILES["fileToUpload"]) && !empty($_FILES["fileToUpload"]['tmp_name'])) {
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$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());
}
}
?>
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报