Can't upload a image from a URI to firebase Storage in PHP-ThrowExceptions

Exception or error:

I’m actually trying to upload a image from its URI to my project’s Firebase Storage place.

Note 1: my access to Firebase seems to be working correctly as i’m able to write & read to my Firestore database using the same SDK. I have installed the whole suit using composer.

Note 2: I am not directly using $_FILES[] as i’m editing my image after it’s been choose. Hence my file’s input datas are not the same as my edited image.

Note 3: When sent back to my form file, the edited image’s URI works perfectly in a tag.

1- In My form file :

<img class="rounded" id="header" src="images/header_placeholder.png" alt="header">
<input type="file" class="sr-only" id="header_input" name="header_img" accept="image/*">
<input id="header_img_url" name="header_img_url" type="hidden" value="">

My hidden input’s value is reassigned with my edited image URI before submitting.

2- In my uploading PHP file (i took away most of the firestore code for clarity)

<?php 
require '../../vendor/autoload.php';
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Core\GeoPoint;
use Google\Cloud\Storage\StorageClient;

function generateRandomString($length = 20) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

$db = new FirestoreClient([
        'projectId' => 'hotpot-XXXXX',
    ]);

# Instantiates a client
$storage = new StorageClient([
    'projectId' => 'hotpot-XXXXX',
]);

$bucket = $storage->bucket('gs://hotpot-XXXXX.appspot.com/');


    $headerImgID = generateRandomString();
    $iconeImgID = generateRandomString();

    $file = fopen($_POST['header_img_url'], 'r');

    $bucket->upload($file, [
        'name' => $headerImgID
    ]);

My script actually crash when the uploading lines are present:

    $bucket->upload($file, [
        'name' => $headerImgID
    ]);

I hope you can help me find where i’m wrong because i have been searching and trying a lot and yet i can’t fix it :/

How to solve:

Ok, i misunderstood what a ‘bucket’ is .. the right way was to just use hotpot-XXXXX.appspot.com without gs://.

Leave a Reply

Your email address will not be published. Required fields are marked *