Reputation: 147
How can i upload file to a specific folder in google drive -Google drive API? I have this code, and it upload my file to the main folder in google drive.
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert($file, array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
));
}
Upvotes: 5
Views: 937
Reputation: 5416
Here's how you do it in V3. It's called "setting a parent" since once the file is inside the folder it will also inherit the sharing options of the parent it's in.
Below 2 lines will set the name of the file and then setParents will define what folder you want to put the file in. Parameter is an array but don't put more than 1 item or you'll get an error:
$file->setName("world");
$file->setParents(['SOME_UNIQUE_FOLDER_ID']);
Upvotes: 1