Google-api-php-client: How to list folders and their childrens

Created on 10 Aug 2017  路  2Comments  路  Source: googleapis/google-api-php-client

I am using this code to get files but how can I access folders and their contents as well their children the documentation is not covering this.
`$client->setAccessToken ($this->getAccessToken ());
$drive = new Google_Service_Drive($client);

            return Response::json (($files = $drive->files->listFiles ()));`

Most helpful comment

Hi,

You have to first list folders, and then explore each folder for files.

The trick to list only folders is to set the q query parameter with the right mimetype :

$drive->files->listFiles(array('q' => "trashed = false AND mimeType='application/vnd.google-apps.folder'"));

The response here will only contain folders in your drive account. Then process in a loop each of this folders to find its files, thanks to the parents property : we select only files having $folder->id as a parent. Said otherwise, we select only $folder->id childs :

// loop content here
$drive->files->listFiles(array('q' => "trashed = false AND '{$folder->id} IN parents' "));
// endloop content

All 2 comments

Hi,

You have to first list folders, and then explore each folder for files.

The trick to list only folders is to set the q query parameter with the right mimetype :

$drive->files->listFiles(array('q' => "trashed = false AND mimeType='application/vnd.google-apps.folder'"));

The response here will only contain folders in your drive account. Then process in a loop each of this folders to find its files, thanks to the parents property : we select only files having $folder->id as a parent. Said otherwise, we select only $folder->id childs :

// loop content here
$drive->files->listFiles(array('q' => "trashed = false AND '{$folder->id} IN parents' "));
// endloop content

Looks like your question was answered by helpful community members. Thanks community! Please feel free re-open if I misread the thread.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dresing picture Dresing  路  3Comments

whatido1 picture whatido1  路  3Comments

kungufli picture kungufli  路  3Comments

bencromwell picture bencromwell  路  3Comments

slaFFik picture slaFFik  路  5Comments