Alejandro
Alejandro

Reputation: 49

How to get the name of a folder in Google Drive PHP API?

I am working on a Google Drive API project with PHP, and I want to show the Title of the folder in which the user is browsing in a breadcrumb for example: Start -> Subfolder

I am using this code to recover files from a folder:

$optParams = array(
      'pageSize' => 999,
                        'fields' => "nextPageToken, files",
                        'q' => "'".$folderId."' in parents"
                    );
                    $results = $service->files->listFiles($optParams);

But how do I get the title of the folder? Any suggestions?

Thanks!

Upvotes: 1

Views: 2982

Answers (1)

Tanaike
Tanaike

Reputation: 201643

  • You want to retrieve a folder name using Drive API.
  • You want to achieve this with Google Client Library using PHP.

I could understand like above. But I'm not sure whether you want to retrieve the folder names in a specific folder or you want to retrieve the folder name from $folderId. So I would like to propose 2 patterns as follows.

Pattern 1:

In this pattern, the folders in a specific folder are retrieved. From the result, you can retrieve the folder names.

Modified script:

$optParams = array(
  'pageSize' => 999,
  'fields' => 'nextPageToken, files',
  'q' => "'".$folderId."' in parents and mimeType = 'application/vnd.google-apps.folder'"
);
$results = $service->files->listFiles($optParams);
  • By adding and mimeType = 'application/vnd.google-apps.folder' to the search query, only folders can be retrieved.

Pattern 2:

In this pattern, the folder is retrieved from $folderId. From the result, you can retrieve the folder names.

Modified script:

$results = $service->files->get($folderId);
printf("%s\n", $results->getName());

Note:

  • This modification supposes that you have already been able to use Drive API. If you want to know how to use Drive API with PHP, please check the Quickstart for PHP.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Edit:

When you want to retrieve the folder ID from the folder name, please use the following script.

Sample script:

$folderName = "folderName"; // Please set the folder name here.
$optParams = array(
  'pageSize' => 10,
  'fields' => 'nextPageToken, files',
  'q' => "name = '".$folderName."' and mimeType = 'application/vnd.google-apps.folder'"
);
$results = $service->files->listFiles($optParams);

Note:

  • In this case, when there are the files of same filenames in the Drive, those are retrieved.

Upvotes: 2

Related Questions