【发布时间】:2023-03-14 05:09:01
【问题描述】:
我正在处理一个 PHP 项目,在该项目中我需要将一个文件(由 HTML 表单提供)上传到 Google Cloud Storage Bucket。表单请求将作为 ajax 请求接收。我的文件上传成功,但是在存储桶中,它显示了一个临时文件,但我已经上传了一个图像文件。
这是我尝试过的:
PHP 文件:
if(isset($_FILES['file'])){
$errors= array();
// Authentication with Google Cloud Platform
$client = new StorageClient(['keyFilePath' => 'api-project-374381085870-eaf930d9ffd7.json']);
$bucket = $client->bucket('storage_client');
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['name'], 'r')
);
}
Ajax 代码:
$(document).on('submit', '#fileForm', function (e) {
e.preventDefault();
var data = new FormData($('#fileForm').get(0));
$.ajax({
type: 'POST',
url: 'gcp_storage.php',
data: data,
processData: false,
contentType: false,
success: function ($data) {
console.log('Succcess');
$('#success').attr('hidden', false);
}
});
});
HTML 代码:
<form id="fileForm" action="" method="post" enctype="multipart/form-data">
<div class="form-row">
<br/>
<div class="form-group">
<label for="textFile">Text File: </label>
<br/>
<input type="file" name="file" id="textFile">
</div>
</div>
<br>
<button type="submit" class="btn btn-lg btn-primary"> Upload </button>
</form>
更新:
现在,我变了:
这个:
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['name'], 'r')
);
收件人:
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r')
);
文件上传成功,但文件扩展名不显示在存储桶内。那么,有没有办法在上传到谷歌云存储桶之前重命名文件?
请帮帮我!
提前致谢!
【问题讨论】:
标签: javascript php ajax google-cloud-storage jquery-file-upload