Praveen Kumar
Praveen Kumar

Reputation: 927

How to get Google drive file by folder name or folder ID?

I am using Google Apis client lib.

I do not understand how to get file list based on folder name or folder id like:-

Drive Folder Structure

Main =>
    - Demo 1
        - img.png
        - img2.png
    - Demo 2
        - img_Demo 2.png
        - img2_Demo 2.png
    - Demo 3
        - img_Demo 3.png
        - img2_Demo 3.png

Now I get Main folder list and id using below code

<?php
// include your composer dependencies
require_once 'vendor/autoload.php';
#https://developers.google.com/api-client-library/php/auth/web-app

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

$client->setAccessToken('AccessToken');

$drive = new Google_Service_Drive($client);

$files_list = $drive->files->listFiles(array())->getFiles($optParams);

echo "<pre>";
print_r($files_list);
exit;
?>

I got this output:

Array
(
    [0] => Google_Service_Drive_DriveFile Object
        (
            [id] => 1LKMiCuI3OlUYrFlQnl
            [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
            [imageMediaMetadataDataType:protected] => 
            [isAppAuthorized] => 
            [kind] => drive#file
            [lastModifyingUserType:protected] => Google_Service_Drive_User
            [mimeType] => application/vnd.google-apps.folder
            [name] => main
            )
        )

Upvotes: 3

Views: 4006

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

File.list has the q paramater which will allow you to search files. The following would return all files that have a parent id of 1234567 that being the file id of the directory you wish to find.

'1234567' in parents

You can send the q param as part of your optParams

$optParams = array( 'q' => "'1234567' in parents" ); 
$files_list = $drive->files->listFiles($optParams); 

Upvotes: 3

Related Questions