使用表单中没有操作文件的表单将文件上传到S3

我正在尝试用 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文件)


有什么建议么?谢谢


繁星淼淼
浏览 124回答 1
1回答

MMMHUHU

添加条件以检查文件是否存在<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><?phpif (isset($_POST['submit']) && !empty($_FILES["fileToUpload"]) &&&nbsp; !empty($_FILES["fileToUpload"]['tmp_name'])) {&nbsp; &nbsp; require 'vendor/autoload.php';&nbsp; &nbsp; use Aws\S3\S3Client;&nbsp; &nbsp; use Aws\S3\Exception\S3Exception;&nbsp; &nbsp; $bucketName = '*********';&nbsp; &nbsp; $IAM_KEY&nbsp; &nbsp; = '********************';&nbsp; &nbsp; $IAM_SECRET = '********************************';&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; $s3 = S3Client::factory(array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'credentials' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'key' => $IAM_KEY,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'secret' => $IAM_SECRET&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'version' => 'latest',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'region' => 'eu-west-1'&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; die("Error: " . $e->getMessage());&nbsp; &nbsp; }&nbsp; &nbsp; $keyName = 'testfile/' . basename($_FILES["fileToUpload"]['name']);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // Uploaded:&nbsp; &nbsp; &nbsp; &nbsp; $file = $_FILES["fileToUpload"]['tmp_name'];&nbsp; &nbsp; &nbsp; &nbsp; $s3->putObject(array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Bucket' => $bucketName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Key' => $keyName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'SourceFile' => $file,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'StorageClass' => 'REDUCED_REDUNDANCY'&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; catch (S3Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; die('Error:' . $e->getMessage());&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; die('Error:' . $e->getMessage());&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP