duanpin9531 2018-04-12 08:01
浏览 72
已采纳

Firebase存储上传php

I'm currently trying to upload to the Firebase Cloud Storage using php, but running into an issue.

I have a form in html, that takes the uploaded file (in my case an image), and passes it to a php file.

This image is very small, 12.8kB, and a JPEG image.

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.

The error I get is,

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

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)

EDIT: When I've changed my code to be

$bucket->upload(
    file_get_contents($_FILES['imageToUpload']['tmp_name']),
    [
        'name' => $_FILES['imageToUpload']['name']
    ]
);

I'm getting the error:

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
  • 写回答

1条回答 默认 最新

  • dppx9253 2018-04-12 11:27
    关注

    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']
        ]
    );
    

    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

    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

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

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决