Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i upload file to a specific folder in google drive -Google drive API (PHP)

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
    ));
}
like image 434
Shir Grinblat Avatar asked Nov 07 '22 02:11

Shir Grinblat


1 Answers

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']);
like image 155
Robert Sinclair Avatar answered Nov 16 '22 14:11

Robert Sinclair