33

I am searching for a way, how can I list files in a specific folder (with folder id) in Google Drive API v3. I used this function $service->files->listFiles() but It return all files in google Drive. Do you know which function can I use?

Bjørson Bjørson
  • 1,583
  • 1
  • 17
  • 31

3 Answers3

60

cool I found the answer.

  $optParams = array(
        'pageSize' => 10,
        'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
        'q' => "'".$folderId."' in parents"
        );
  $results = $service->files->listFiles($optParams);
Bjørson Bjørson
  • 1,583
  • 1
  • 17
  • 31
  • 5
    Also appears to work in the JS lib. Now to figure out where Google actually tells us this should work! Ah! Google, use HTML links! You know what they are! Link to the Search Guide! https://developers.google.com/drive/v3/reference/files/list Here's the q params: https://developers.google.com/drive/v3/web/search-parameters – Virmundi Oct 20 '17 at 00:16
  • 3
    It worked! For people using python, remember the quote the folder id `q='FOLDER_ID' in parents` – Steven Xu Apr 08 '20 at 12:49
0

There is a caveat to this that I've found. If I'm not an owner of the folder and have not opened a file, this method will not show it to me. That is, if I browse a folder in Chrome, I can see all of the files in it, but if I use the API, I only get back the two or three files that I'd previously opened in the browser.

Updated: I figured out why I was getting this result. I had used

q=f"parents in '{folder_id}'"
instead of
q=f"'{folder_id}' in parents"
Why did that return anything at all? Who knows?
0

This code is working perfectly for me.

$optParams = array(
    'corpora' => "allDrives",
    'pageSize' => 100,
    'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
    'q' => "'".$folderId."' in parents",
    'includeItemsFromAllDrives' => 'true',
    'supportsAllDrives' => 'true'
    );
$file = $driveService->files->listFiles($optParams);
Aupire
  • 11
  • 1