2017-03-06 55 views
1

我是CodeIgniter的新手,我正在做一個將CI應用程序與Amazon S3集成的poc。我使用了github的S3 PHP類(https://github.com/psugand/CodeIgniter-S3),並能夠列出存儲桶和列表對象以查看文件。 我的問題是首先我不能從視圖窗體中將值發佈到控制器,然後需要檢查如何將文件上傳到s3。也很好用在putObject函數中使用URI是什麼?在CodeIgniter中表單發佈並上傳到AWS S3不起作用

以下是控制器&查看文件。請讓我知道我們如何形成發佈到控制器,然後上傳到S3。

我的控制器的文件 - S3demo.php

class S3demo extends CI_Controller { 

    public function index() 
    { 
     //Var Declaration 
     $data['bucketName'] = "demacrtest"; 

     // Load Library 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
     $this->load->library('s3'); 

     // Get all required Data 
     $data['bucket_list'] = $this->s3->listBuckets(); 
     $header_data['title'] = "CI and S3 Demo"; 
     $data['object_list'] = $this->s3->getBucket($data['bucketName']); 


     // Load Views 
     $this->load->view('common/header', $header_data); 
     $this->load->view('s3_demo', $data); 
     $this->load->view('common/footer'); 
    } 

    public function doUploadtoS3(){ 
     echo "in"; 

     //Var Declaration 
     $data['bucketName'] = "demacrtest"; 
     $uri = "dms/"; 
// Load Library 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
     $this->load->library('s3'); 

     // GET DATA POST FROM FORM 
     $data = array(
     'files' => $this->input->post('uploadedfiles') 
     ); 
     // DISPLAY DATA 
     echo "<pre>"; 
     print_r($data); 

     // PUT with custom headers: 
     $put = S3::putObject(
      S3::inputFile($data), 
      $data['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")) 
      ) 
     ); 
    } 
} 

我查看文件 - s3_demo.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> 

+0

還試圖用CI表單功能,但它是沒有用的 '<?PHP的回聲form_open( 's3demo/doUploadtoS3')?>' 即使這是不工作... –

回答

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); 
} 
+0

的幫助我的網站[CI LINK](https://www.codeigniter.com/userguide3/libraries/file_uploading.html) [; S3 LINK](http://undesigned.org.za/2007/10/22/亞馬遜-S3-PHP-類/文檔#putObject) –