Sunday 29 May 2022

Upload Files On Google Drive Using PHP API

 


Upload Files On Google Drive Using PHP API

-------------------------------

Google Drive API Part 1:-
https://youtu.be/etZFZPKJc_I

Stack Overflow Link :-

https://stackoverflow.com/questions/25707891/google-drive-php-api-simple-file-upload


Subscribe my Channel:- 

http://www.youtube.com/user/SanketRooney?sub_confirmation=1 


Facebook Page:- 

https://www.facebook.com/UnpossibleNS 


Twitter Account:- 

https://twitter.com/UnpossiblePOG 


Blog:- 

https://unpossiblepog.blogspot.com/ 


Website:- 

https://unpossiblepog.com/ 

Gaming Instagram:- (@unpog.gaming)

https://www.instagram.com/unpog.gaming/


-------------------------------

upload-file.php

-------------------------------


<?php

require __DIR__ . '/vendor/autoload.php';


$client = new Google_Client();


// Get your credentials from the console


$client->setClientId('<YOUR_CLIENT_ID>');


$client->setClientSecret('<YOUR_CLIENT_SECRET>');


$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');


$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));





session_start();





if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {


if (isset($_GET['code'])) {


$client->authenticate($_GET['code']);


$_SESSION['access_token'] = $client->getAccessToken();


} else


$client->setAccessToken($_SESSION['access_token']);





$service = new Google_Service_Drive($client);





//Insert a file


$file = new Google_Service_Drive_DriveFile();


$file->setName(uniqid().'.jpg');


$file->setDescription('A test document');


$file->setMimeType('image/jpeg');





$data = file_get_contents('a.jpg');





$createdFile = $service->files->create($file, array(


'data' => $data,


'mimeType' => 'image/jpeg',


'uploadType' => 'multipart'


));





print_r($createdFile);





} else {


$authUrl = $client->createAuthUrl();


header('Location: ' . $authUrl);


exit();


}


?>


-------------------------------




In last part, we were able to list files from google drive.


In this part, we shall upload files from our server to google drive.


Let’s jump right into it.


I got a code from stack-overflow, you can copy-paste the code from their link or from my blog. Both links are given in the description.


Create a php file inside a google drive folder which we created last time.


Paste the code inside it within php tags.


We have a vendor folder ready, so remove first 2 lines & import the library.


For client id and client secret. We have to look into credentials.json file.


Also make sure that we first ran quickstart file, the scope was DRIVE.


If not, delete credentials.json file and redo steps again.


Inside json file, we shall find client-id and client-secret.


Copy-paste them inside our code.


In console cloud google site, first select the project.


Then go inside credentials inside APIS & SERVICES.


Select the authentication ID, inside it, add extra URL.


Use the name of newly created php file and save it.


As per code, we shall upload a.png file so change the code accordingly.


Now hit our file in URL.


There is an error.


Change the file path to absolute. Save it.


Add the file path.


After refresh, it will print some array.


And if you go inside your google drive, you will find that image.

Thanks for watching.