$ s3-> putObject似乎使我的PHP文件的其余部分像纯字符串一样

时间:2016-05-04 15:16:22

标签: php amazon-web-services amazon-s3

我是PHP的新手,我正在尝试创建一个将文件上传到S3存储桶的网页。

我的脚本如下所示:

<? php

use Aws\S3\Exception\S3Exception;

require 'start.php';

//Global files variable does it contain something?
if (isset($_FILES['file'])){

    $file = $_FILES['file']; //This relates to the file we're uploading.

    //File details
    $name = $file['name'];
    $tmp_name = $file['tmp_name'];

    $extension = explode('.', $name); //Getting the extension of the file.
    $extension = strtolower(end($extension));

    //Giving the files some temp info
    $key = md5(uniqid()); #Generating a random file extension
    $tmp_file_name = "{$key}.{$extension}";
    $tmp_file_path =  "{$tmp_file_name}";

    var_dump($tmp_file_path);

    //Move the file
    move_uploaded_file($tmp_name, $tmp_file_path);

    try {

        //Accessing config variable
        $s3->putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => "uploads/{$name}", //Where we actually store this on amazon S3. CAN USE $TMP_NAME
                'Body' => fopen($tmp_file_path, 'rb'), //Resource, open files - rb is reading.
                'ACL' => 'public-read' //Access control level, permissions as such
            ]);

        //Remove the file
        unlink($tmp_file_path);

    } catch (S3Exception $e) {
        die( $e->getMessage(), "\n");
    }
}

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Upload</title>
    </head>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" value="Upload">
        </form>
    </body>
</html>

但是,我的网页看起来像这样

Web page

网页应该只显示按钮,我无法解决为什么代码在$ s3->之后?也在我的网页上显示,而不是做它应该做的事情。

谁能给我一个关于我做错了什么的线索?感谢。

0 个答案:

没有答案