Create a folder in google drive php

Faouzi FJTech picture Faouzi FJTech · Aug 1, 2012 · Viewed 10.7k times · Source

Is there any way we could create a folder on google drive using PHP? I've been searching all the web for the last 72 hours with no results. What do you think guys? Thanks !

Answer

krchun picture krchun · Oct 29, 2019

Here is an updated method using the Google Drive API v3 and an OAuth access token:

$googleClient = new \Google_Client();
$googleClient->setApplicationName('My App');
$googleClient->setAccessToken(env('GOOGLE_OAUTH_ACCESS_TOKEN'));
$googleClient->setClientId(env('GOOGLE_APP_ID'));
$googleClient->setClientSecret(env('GOOGLE_APP_SECRET'));

if ($googleClient->isAccessTokenExpired()) {
    $googleClient->fetchAccessTokenWithRefreshToken();
}

$api = new \Google_Service_Drive($client);

$file = new \Google_Service_Drive_DriveFile();
$file->setName('Folder Name');
$file->setMimeType('application/vnd.google-apps.folder');

$folder = $api->files->create($file);