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 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示