0

I'm trying to develop a PHP script which goes through my Google Drive to list my files . I have the current structure :

 -- logo1.png
|
|-- logo2.png
|
 -- myFolder
|     |
|      --logo3.png
|
|
 -- myPrivateFolder
      |
       --logo4.png

I'm using the Google PHP library to make my queries. To list my files, I have this code :

$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);


$result = array();

try {
  $parameters = array(
      'q' => "mimeType contains 'image/'",
  );
  $files = $service->files->listFiles($parameters);

  $result = array_merge($result, $files->getFiles());
} catch (Exception $e) {
  print "Une erreur s'est produite : " . $e->getMessage();
}
return $result;

$result is an array of 4 elements. Since I specified I only want MIME type 'image/', I only get my 4 images. Now, I'd like to know, how can I filter my results to exclude my private directory from the research ? I want to be able to specify in which folder not to look ?

I wasn't able to find in the documentation anything about that. Thanks in advance for any help !

saperlipopette
  • 1,603
  • 12
  • 26

1 Answers1

2

Firstly you'll need to get the ID of your private directory. You can do that with a query

name = 'my_private_directory'

Once you have the ID, you can extend your existing query with

and not 'xxxxxx' in parents where xxxx is the File ID from step 1.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks for your answer ! Working great. If I have more folders, how can I adapt my request ? `mimeType contains 'image/' and not 'X1' in parents and not 'X2' in parents and not X3' in parents and not 'X4' in parents` – saperlipopette Jul 06 '17 at 07:47
  • Also I'd like to take only my files, not the sharedWithMe files. Thus, I add `and sharedWithMe = false` but i got this error : `{ "error": { "errors": [ { "domain": "global", "reason": "invalid", "message": "Invalid Value", "locationType": "parameter", "location": "q" } ], "code": 400, "message": "Invalid Value" } }` – saperlipopette Jul 06 '17 at 08:03
  • `mimeType contains 'image/' and not 'X1' in parents and not 'X2' in parents and not X3' in parents and not 'X4' in parents and sharedWithMe = false` But if i write `= true` it works, but i'd like it to be set to false... – saperlipopette Jul 06 '17 at 09:45
  • looks like a bug in the Drive API – pinoyyid Jul 06 '17 at 12:11
  • Ok thank you very much about your help. Do you know If I can apply my script to multiple accounts ? I mean instead of opening my PHP page in my browser which makes the ListFiles search, can I have a server side php script that lists the files of multiple google accounts ? – saperlipopette Jul 06 '17 at 13:09
  • yes. See https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic/19766913#19766913 – pinoyyid Jul 06 '17 at 13:44
  • Hello, It's me again ^^ I invite you to imagine this situation : folder1 --> folder2 --> logo.png If I exclude folder2 from my query, logo.png won't be returned since it has folder2 as a direct parent, but what if I want to exclude folder1 and all his children ? – saperlipopette Aug 06 '17 at 02:46
  • When dealing with folders, I find it's better to start by fetching all folders and building an in memory hierarchy of folders. Then construct your query using the appropriate "xxxx in parents", "xxxx not in parents" query. Drive has no concept of a grandparent. – pinoyyid Aug 06 '17 at 09:20
  • So I in my exemple if I want to exclude folder1 and all of its content, I will need in my program to run a sort of loop to check all children of folder1 and then get their ID to add this ID in a new 'not in parents' clause ? – saperlipopette Aug 06 '17 at 11:18
  • i find it easier to fetch ALL folders in a single files.list. Then build your folder hierarchy in a memory data structure, then construct the query you need – pinoyyid Aug 06 '17 at 17:44
  • I couldn't achieve to set a hierarchy directly in a structure, too difficult for my level I guess. I made a recursive function, for a given folder, it list all children which mimeType are folders, again and again to get the IDs of all folders under a given folder. I finally get my script working. I'd like to thank you for your patience and for all the time you granted me. The GDrive API is really awesome and i'm going to practice more with it. Thank you again and again :-) Have a nice day ! – saperlipopette Aug 06 '17 at 21:20
  • I have a new error : 413 => Your client issued a request that was too large. If my headfolder has about 150 subfolders, how can I exclude my headfolder without excluding my subfolders ? I have to make separate requests ? – saperlipopette Aug 07 '17 at 09:38
  • ask as a new question with code extract and details – pinoyyid Aug 07 '17 at 10:18
  • https://stackoverflow.com/questions/45545616/files-list-with-an-exclusion-of-folders-and-all-its-grand-children-subdirectorie – saperlipopette Aug 07 '17 at 11:21