Firebase Storage upload php

TallGuyRob

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
jeromegamez

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Firebase Storage upload synchronously

From Dev

Firebase Storage upload synchronously

From Dev

Firebase Storage invalid argument on upload

From Dev

Firebase storage - Limit size of image that users upload to firebase storage

From Dev

Firebase storage - Limit size of image that users upload to firebase storage

From Dev

Firebase: Storage upload file size limit?

From Dev

How to observe Firebase Storage Upload event

From Dev

Upload image from URL to Firebase Storage

From Dev

How to check progress of image upload to Firebase storage

From Dev

Firebase Storage Upload Error: FIRStorageErrorDomain Code=-13000

From Dev

Upload Facebook image URL to Firebase Storage

From Dev

Firebase Storage - Upload multiple image files in Android

From Dev

Not able to upload images to Firebase Cloud Storage

From Dev

Upload into specific folder Firebase Storage NodeJS?

From Dev

Firebase Storage Upload Error: FIRStorageErrorDomain Code=-13000

From Dev

Upload Facebook image URL to Firebase Storage

From Dev

Upload multiple file with different extension firebase storage

From Dev

Image Size Changed After Upload To Firebase Storage

From Dev

Upload Images to Firebase Storage and store a link in Firebase Database

From Dev

google storage cloud upload file using php

From Dev

PHP Softlayer Object Storage Upload SSL error

From Dev

Angular 2 Firebase Storage - Image preview on upload finish

From Dev

Upload image to Firebase Storage from django models in FileField

From Dev

React-native Firebase storage upload using putString call

From Dev

How to upload images to firebase storage and wait for URLs to return before continuing

From Dev

Create a text file with Textfield data and upload to firebase storage in Flutter

From Dev

deferred/promise js mecanism on multiple firebase storage upload

From Dev

Upload image taken with camera to firebase storage on React Native

From Dev

Ionic 2 app: Same image upload in firebase storage

Related Related

  1. 1

    Firebase Storage upload synchronously

  2. 2

    Firebase Storage upload synchronously

  3. 3

    Firebase Storage invalid argument on upload

  4. 4

    Firebase storage - Limit size of image that users upload to firebase storage

  5. 5

    Firebase storage - Limit size of image that users upload to firebase storage

  6. 6

    Firebase: Storage upload file size limit?

  7. 7

    How to observe Firebase Storage Upload event

  8. 8

    Upload image from URL to Firebase Storage

  9. 9

    How to check progress of image upload to Firebase storage

  10. 10

    Firebase Storage Upload Error: FIRStorageErrorDomain Code=-13000

  11. 11

    Upload Facebook image URL to Firebase Storage

  12. 12

    Firebase Storage - Upload multiple image files in Android

  13. 13

    Not able to upload images to Firebase Cloud Storage

  14. 14

    Upload into specific folder Firebase Storage NodeJS?

  15. 15

    Firebase Storage Upload Error: FIRStorageErrorDomain Code=-13000

  16. 16

    Upload Facebook image URL to Firebase Storage

  17. 17

    Upload multiple file with different extension firebase storage

  18. 18

    Image Size Changed After Upload To Firebase Storage

  19. 19

    Upload Images to Firebase Storage and store a link in Firebase Database

  20. 20

    google storage cloud upload file using php

  21. 21

    PHP Softlayer Object Storage Upload SSL error

  22. 22

    Angular 2 Firebase Storage - Image preview on upload finish

  23. 23

    Upload image to Firebase Storage from django models in FileField

  24. 24

    React-native Firebase storage upload using putString call

  25. 25

    How to upload images to firebase storage and wait for URLs to return before continuing

  26. 26

    Create a text file with Textfield data and upload to firebase storage in Flutter

  27. 27

    deferred/promise js mecanism on multiple firebase storage upload

  28. 28

    Upload image taken with camera to firebase storage on React Native

  29. 29

    Ionic 2 app: Same image upload in firebase storage

HotTag

Archive