Search code examples
phpgoogle-drive-apigoogle-api-php-client

How to check with google/apiclient if a directory with a specified id exists in Google Drive?


I tried:

count($drive->files->listFiles([
    'q' => "mimeType='application/vnd.google-apps.folder' and id='$driveDirId'"
])['files']) > 0

but Drive query language doesn't seem to have an id term

I would only need checking for directories I have access to, of course.


Solution

  • Ended up doing this:

    $driveFile = $drive->files->get($driveDirId, [
        'supportsAllDrives' => true,
    ]);
    if ($driveFile === null) {
        return false;
    }
    if ($driveFile->getMimeType() === 'application/vnd.google-apps.folder') {
        return true;
    } else {
        throw new Error("$driveDirId is not a directory");
    }