Firebase存储上传PHP [英] Firebase Storage upload php
问题描述
我目前正在尝试使用php上传到Firebase Cloud Storage,但是遇到了问题.
I'm currently trying to upload to the Firebase Cloud Storage using php, but running into an issue.
我有一个使用html的表单,该表单将上载的文件(在我的情况下为图像)传递给php文件.
I have a form in html, that takes the uploaded file (in my case an image), and passes it to a php file.
此图像非常小,只有12.8kB,是JPEG图像.
This image is very small, 12.8kB, and a JPEG image.
php文件获取图像,然后尝试将其上传到存储. 我当前的php代码大致为:
The php file takes the image, and tries to upload it to the storage. My current php code is roughly:
$storage = new StorageClient(['projectId' => '<my_project_id> ']);
$bucket = $storage->bucket('images');
$bucket->upload($_FILES['imageToUpload']['tmp_name']);
我可以得到它来打印存储桶名称,它是详细信息,所以我知道不是引起错误的那部分.
I can get it to print the bucket name, and it's details, so I know it's not that part that is causing the error.
我得到的错误是,
Fatal error: Uncaught InvalidArgumentException: A name is required when data is of type string or null.
in /var/www/html/Images/vendor/google/cloud-storage/src/Bucket.php:258
Stack trace: #0 /var/www/html/Images/uploadImage.php(74): Google\Cloud\Storage\Bucket->upload('/tmp/phpA9SAM8')
#1 {main} thrown in /var/www/html/Images/vendor/google/cloud-storage/src/Bucket.php on line 258
哦,如果要紧的话,我也正在使用apache2在16.04 Ubuntu上工作. (但是我已经能够使用php上传到我自己的计算机上.只需重构即可使用Firebase)
Oh, I am also working on 16.04 Ubuntu if that matters, with apache2. (But I've been able to upload to my own computer with php. Just refactoring to use Firebase now)
当我将代码更改为
$bucket->upload(
file_get_contents($_FILES['imageToUpload']['tmp_name']),
[
'name' => $_FILES['imageToUpload']['name']
]
);
我遇到了错误:
Fatal error: Uncaught Google\Cloud\Core\Exception\NotFoundException: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "Not Found" } ], "code": 404, "message": "Not Found" } } in /var/www/html/Images/vendor/google/cloud-core/src/RequestWrapper.php:263
Stack trace: #0 /var/www/html/Images/vendor/google/cloud-core/src/RequestWrapper.php(168): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException))
#1 /var/www/html/Images/vendor/google/cloud-core/src/Upload/MultipartUploader.php(64): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array)
#2 /var/www/html/Images/vendor/google/cloud-storage/src/Bucket.php(268): Google\Cloud\Core\Upload\MultipartUploader->upload()
#3 /var/www/html/Images/uploadImage.php(75): Google\Cloud\Storage\Bucket->upload('/tmp/phpR2vYYg', Array)
#4 {main} thrown in /var/www/html/Images/vendor/google/cloud-core/src/RequestWrapper.php on line 263
推荐答案
通过使用$_FILES['imageToUpload']['tmp_name']
,您将上载图像的临时名称用作内容,而不是实际的图像文件.
By using $_FILES['imageToUpload']['tmp_name']
, you are using the temporary name of the uploaded image as the contents, not the actual image file.
解决此问题的快捷方法是使用:
The quickes way to solve this is to use:
$bucket->upload(
file_get_contents($_FILES['imageToUpload']['tmp_name']),
[
'name' => $_FILES['imageToUpload']['name']
]
);
upload
方法接受方法的PHPDoc中所述的一系列选项(包括目标文件名):
The upload
method accepts an array of options (including the target file name) as described in the method's PHPDoc: https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/Storage/src/Bucket.php#L216
请记住,直接使用上载的文件名(而不是tmp_name
)会带来安全隐患,因此请确保在将上载的文件移至云存储之前对其进行验证和清理.
Please keep in mind though that there are security implications when using the uploaded file name (not the tmp_name
) directly, so please make sure to validate and sanitize the uploaded files before moving them to your cloud storage.
http://php.net/manual/en /features.file-upload.post-method.php http://php.net/manual/en/function.move- Uploaded-file.php
这篇关于Firebase存储上传PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!