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
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With