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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (275)

我正在尝试使用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';

?>

任何帮助表示赞赏。谢谢!

提问于
用户回答回答于

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

扫码关注云+社区

领取腾讯云代金券