注意:未定义的索引:第46行的C:\ wamp \ www \ upload.php中的fileToUpload

时间:2018-06-14 22:03:24

标签: php html amazon-s3

我正在尝试使用php将图像文件上传到AWS S3服务器。但是,我一直收到这个错误。

Notice: Undefined index: fileToUpload in C:\wamp\www\upload.php on line 46

Notice: Undefined index: fileToUpload in C:\wamp\www\upload.php on line 51

这是我的代码:

HTML(文件名:up.html):

{<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" ectype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP(文件名:upload.php)

<?php

    require '../vendor/autoload.php';

    use Aws\S3\S3Client;
    use Aws\S3\Exception\S3Exception;
    // AWS Info
    $bucketName = 'bakemonoden1';
    $IAM_KEY = '******************';
    $IAM_SECRET = '***************************************';
    // Connect to AWS
    try {

        $s3 = S3Client::factory(
            array(
                'credentials' => array(
                    'key' => $IAM_KEY,
                    'secret' => $IAM_SECRET
                ),
                'version' => 'latest',
                'region'  => 'ca-central-1'
            )
        );
    } catch (Exception $e) {

        die("Error: " . $e->getMessage());
    }

    $keyName = 'test_example/' . basename($_FILES["fileToUpload"]['tmp_name']);
    $pathInS3 = 'https://s3.ca-central-1.amazonaws.com/' . $bucketName . '/' . $keyName;
    // Add it to S3
    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());
    }
    echo 'Done';

?>

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

表单中有错误,您忘记了ectype中的'n'=“multipart / form-data”是enctype