【发布时间】:2019-04-02 13:58:00
【问题描述】:
我正在使用 Codeigniter 应用程序并尝试为用户提供上传文件的访问权限。服务器设置在运行 CentOS 7 和 Apache 2 的 Google Compute Engine VM 上,我正在尝试使用 Google Cloud Storage 进行用户上传。目前,当我上传文件时,只有文件名被上传到 GCS Bucket,文件大小为 0 字节。
我的前端代码:
<body>
<form action="/includes/includes_gc/do_upload" class="dropzone" id="pfUploads" enctype="multipart/form-data">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script>
<script>
Dropzone.options.pfUploads = {
paramName: "file",
uploadMultiple: true,
init: function () {
this.on('complete', function (data) {
console.log(data);
});
}
};
</script>
</body>
控制器功能:
public function do_upload() {
if ($_FILES) {
$filename = $_FILES['file']['name'];
$gs_name = file_get_contents($_FILES['file']['tmp_name']);
$bucketName = 'xxxxxx.appspot.com';
$kdir = dirname(getcwd(), 2);
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
'projectId' => 'xxxxxx'
]);
$file = fopen($gs_name, 'r');
$bucket = $storage->bucket($bucketName);
$bucket->upload($file, [
'name' => $filename
]);
$test = array();
array_push($test, basename($gs_name));
array_push($test, $bucketName);
array_push($test, $filename);
echo json_encode($test);
}
}
对于解决此问题的任何帮助,我将不胜感激。
提前致谢。 纳文
【问题讨论】:
标签: codeigniter google-app-engine upload google-cloud-storage