Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file into particular folder on Google Drive using PHP and Drive API

Hope this is an easy one... I want to upload file directly into a folder. I got folder ID and I have this code that uploads file in the root perfectly:

<?php
require_once 'get_access.php'; 

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$folder_id = '<folder id>';

$client = new Google_Client();
$client->setClientId('<client_id>');
$client->setClientSecret('<client_secret>');
$client->setRedirectUri('<url>');
$client->setScopes(array('<scope>'));
$client->setAccessToken($access);
$client->setAccessType('offline');
$client->refreshToken($refreshToken);
$service = new Google_DriveService($client);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document 123');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document-2.txt');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

echo '<pre>';
var_dump($createdFile);
echo '</pre>';

?>

What should I do here in order to upload this file into particular folder on user's drive?

Thanks

like image 612
Vlada Janjušez Avatar asked Dec 04 '22 12:12

Vlada Janjušez


1 Answers

I guess this is crazy, but Google has changed the API yet again! The ParentReference class is now Google_Service_Drive_ParentReference

So the updated code is:

$parent = new Google_Service_Drive_ParentReference();
$parent->setId($folderId);
$file->setParents(array($parent));
like image 163
raidenace Avatar answered Dec 09 '22 14:12

raidenace