Form Post并上传到CodeIgniter中的AWS S3无法正常工作

时间:2017-03-06 06:37:29

标签: php codeigniter amazon-s3

我是CodeIgniter的新手,我正在做一个将CI应用程序与Amazon S3集成的poc。我使用了来自github(https://github.com/psugand/CodeIgniter-S3)的S3 PHP类,并且能够从桶中列出桶和列表对象以查看文件。 我的问题首先是我无法从视图表单中将值发布到控制器,然后需要检查如何将文件上传到s3。另外,在putObject函数中使用URI有什么用处?

以下是Controller&查看文件。请告诉我们如何将表格发布到控制器,然后再上传到S3。

我的控制器文件 - S3demo.php

<div class="container-fluid">
<div class="row">
    <div class="col-sm-6">
        <h1>List of S3 buckets</h1>
        <div>
            <ul>
                <?php foreach ($bucket_list as $bucket){?>
                    <li><?php echo $bucket;?></li>
                <?php  }?>
            </ul>
        </div> 
    </div>
    <div class="col-sm-6">
        <h1>List of objects in bucket - <?php echo $bucketName; ?></h1>
        <div>
            <ul>
                <?php foreach ($object_list as $objects){?>
                    <li><?php echo $objects['name']; ?></li>
                <?php  }?>
            </ul>
        </div>
    </div> 
</div>
 <div class="row">
        <div class="upload-form">
            <h3>Upload to S3 Form</h3>
            <form class="form-horizontal" role="form" action="s3demo/doUploadtoS3" method="post" enctype="multipart/form-data">
               <div class="form-group">
                    <label for="name" class ="control-label col-sm-3">Upload</label>
                    <div class="col-sm-8">
                      <input id="input-2" name="uploadedfiles[]" type="file" class="file">
                    </div>
                </div>
                <div class="col-sm-offset-2 col-sm-8">
                 <button type="submit" class="btn btn-default">Upload</button>
               </div>
            </form>
        </div>
</div>  

我的浏览文件 - s3_demo.php

where

2 个答案:

答案 0 :(得分:0)

最后工作......改变了uploadtos3功能。 $this->upload->do_upload('userfile');让我工作

 public function doUploadtoS3(){

    //Var Declaration
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 1000;

    // Load Library
    $this->load->library('upload', $config);
    $this->load->library('s3');

    if ( ! $this->upload->do_upload('uploadedfiles'))
    {
        $error = array('error' => $this->upload->display_errors());
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    }
    // CONSTRUCT URI
    $uri = "dms/images/".time()."-".$data['upload_data']['file_name'];
    $bucketName = "demacrtest";

    // DISPLAY DATA
    echo "<pre>";
    print_r($data);

    // PUT with custom headers:
    $put = S3::putObject(
        S3::inputFile($data['upload_data']['full_path']),
        $bucketName,
        $uri,
        S3::ACL_PUBLIC_READ,
        array(),
        array( // Custom $requestHeaders
            "Cache-Control" => "max-age=315360000",
            "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
        )
    );
    var_dump($put);
}

答案 1 :(得分:0)

// PUT with custom headers:
$put = S3::putObject(
    S3::inputFile($data['upload_data']['full_path']),
    $bucketName,
    $uri,
    S3::ACL_PUBLIC_READ,
    array(),
    array( // Custom $requestHeaders
        "Cache-Control" => "max-age=315360000",
        "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
    )
);
var_dump($put);