Skip to content

Instantly share code, notes, and snippets.

@imselmon
Last active June 5, 2022 15:34
Show Gist options
  • Save imselmon/b54b2593e2e0138aa8b4c5643863170a to your computer and use it in GitHub Desktop.
Save imselmon/b54b2593e2e0138aa8b4c5643863170a to your computer and use it in GitHub Desktop.
PHP Upload file in Google Cloud Storage & Firebase Storage using Firebase PHP SDK
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
$storage = (new Factory())
->withServiceAccount('Service-Account.json')
->withDefaultStorageBucket('storage.appspot.com')
->createStorage();
$bucket = $storage->getBucket();
?>
<form action="" method="POST" enctype="multipart/form-data" name="ex_card" class="forms-sample">
<div class="form-group">
<label>College or University Logo</label>
<input type="file" name="education_img1x" class="file-upload-default">
</div>
<button type="submit" name="Add_education" class="btn btn-primary me-2">Update</button>
</form>
<?php
session_start();
include 'config.php';
if(isset($_POST['Add_education'])){
if($_FILES['Education_img1x']['name']){
$bucket->upload(
file_get_contents($_FILES['Education_img1x']['tmp_name']),
[
'name' =>"Education_img".$_FILES['Education_img1x']['name']
]
);
}
?>
@imselmon
Copy link
Author

imselmon commented Nov 6, 2021

It Needs the Firebase PHP SDK

@arjansayadi13
Copy link

how will i retrieve na name of the files i have uploaded sir?

@imselmon
Copy link
Author

imselmon commented Jun 5, 2022

how will i retrieve na name of the files i have uploaded sir?
Send a get request to this URL, it will give you an json with items if your storage
https://firebasestorage.googleapis.com/v0/b/coder-salman.appspot.com/o/

Response

image

/coder-salman.appspot.com/ -> replace with your own bucket URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment