dongxue9997 2013-12-06 22:45
浏览 62
已采纳

使用PHP将文件上传到Amazon S3最终会得到一个28字节的文件

I am working on a project where users can upload files to the site that is stored in an Amazon S3 bucket.

This doesn't really work though.. The server sees the file and everything seems good, apart from the file only being 28 bytes big when it ends up on amazon..

This is my code so far:

controller

public function uploadFile()
{
    $data['username'] = $this->input->cookie('username', TRUE);
    $data['pagetitle'] = "Upload file";
    $data['userid'] = $this->userid;

    $this->load->template('uploadFile', $data);
}

public function takeUpload()
{
    $config['upload_path'] = './images/screenshots/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['encrypt_name'] = TRUE;

    $this->load->library('upload', $config);

    $projectFileLoc = $_FILES['projectFile']['tmp_name'];
    $projectFileName = $_FILES['projectFile']['name'];

    if (!($this->upload->do_upload("previewImage")) && !($this->s3->putObject($projectFileLoc, '3dnation', $projectFileName, $this->s3->ACL_PUBLIC_READ)))
    {
        echo "Something went wrong...";
    }
    else
    {
        $imgData = $this->upload->data();
        $previewImage = $imgData['file_name'];
        //TODO: Add image to DB
    }
}

The view file (uploadFile.php)

  <div class="row">
    <div class="col-xs-12 col-sm-12">
      <div class="jumbotron">
        <h1>Upload files</h1>
      </div>
    </div><!--/span-->
  </div><!--/row-->

  <div class="row">
    <div class "col-xs-12 col-sm-12">
        <form action="/home/takeUpload" method="POST" role="form" class="form-horizontal" enctype="multipart/form-data">
              <div class="form-group">
                <label for="title" class="col-sm-2 control-label">Title</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" id="title" placeholder="Title" name="title" required>
                </div>
              </div>
              <div class="form-group">
                <label for="description" class="col-sm-2 control-label">Description</label>
                <div class="col-sm-10">
                  <input type="textfield" class="form-control" id="description" placeholder="Enter a description here" name="description" required>
                </div>
              </div>
              <div class="form-group">
                <label for="projectFile" class="col-sm-2 control-label">Project/Scene files (accepted formats: zip, rar, 3ds)</label>
                <div class="col-sm-10">
                  <input type="file" class="form-control" id="projectFile" name="projectFile" required>
                </div>
              </div>
              <div class="form-group">
                <label for="previewImage" class="col-sm-2 control-label">Preview image</label>
                <div class="col-sm-10">
                  <input type="file" class="form-control" id="previewImage" name="previewImage" required>
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" class="btn btn-default">Upload</button>
                </div>
              </div>
        </form>
    </div>
  </div>

I am building it with the latest Codeigniter with this S3 library

What is wrong?

  • 写回答

1条回答 默认 最新

  • doulianxing4015 2013-12-06 22:57
    关注

    You must call to putObject with an array, if you want use a file you must call too "putObjectFile" function:

    Check the definition in the class:

    public static function putObjectFile($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null)
    {
         return self::putObject(self::inputFile($file), $bucket, $uri, $acl, $metaHeaders, $contentType);
    }
    

    A example from the original library:

    // Simple PUT:
    if (S3::putObject(S3::inputFile($file), $bucket, $uri, S3::ACL_PRIVATE)) {
        echo "File uploaded.";
    } else {
        echo "Failed to upload file.";
    }
    

    As you can see in the doc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果