Skip to content
Advertisement

How to get all folders & files based on Owner using PHP with Google Drive API

How to get all files and folder based on owner?

https://imgur.com/gallery/uf8nXOX

I tried this query but it is just listing all sharedWithMe :

<?php
        
        //fields can be use files(id, name, modifiedTime, mimeType, parents, trashed, permissions, webViewLink, webContentLink, exportLinks), nextPageToken
        
        $client = new Google_Client();
        $client->setApplicationName('Google Drive API PHP Quickstart');
        $client->setAuthConfig('gdrive/json/'. $_SESSION['json'] . '.json');
        $client->setScopes(Google_Service_Drive::DRIVE);
        $client->setSubject($_SESSION['json_email']);
        $client->setAccessType('offline');
                
        
            $service = new Google_Service_Drive($GLOBALS['client']);
            
            $parameters['pageSize'] = 250;
            $parameters['q'] = "sharedWithMe and mimeType='application/vnd.google-apps.folder'"; 
            //"mimeType='application/vnd.google-apps.folder' and 'root' in parents and trashed=false";
            $parameters['fields'] = 'files(id, name,modifiedTime,owners), nextPageToken';
            //'files(id, name, modifiedTime, mimeType, parents, trashed, permissions, webViewLink, webContentLink, exportLinks), nextPageToken'; 
                
            $files = $service->files->listFiles($parameters);
        
            foreach( $files as $k => $file ){
            echo " <tr>
                            <td><large><i class='ti-folder'></i></large>
                                ";
                $forlinkowners = $file['owners'];
                        foreach ($forlinkowners as $pp => $getowner2){
                            echo "<a href='rmsystems.php?shared&&id=".$file['id']."&&foldername=".$file['name']."&&email=".$getowner2['emailAddress']."'>";
                    
                            echo folder_from_rename($file['name']) . "&nbsp&nbsp<span class='badge badge-primary'>link</span>";
                                
                            echo "</a></td>
                             <td><i class='ti-crown'></i>&nbsp&nbsp".retrieve_name($getowner2['emailAddress']).
                                     "</td>"; 
                            }
                                echo "</tr> ";
                            }
            ?>

I just want to group them by their Owner. Any suggestions? How will I do it in PHP or Should I query it in google API?

Thanks a lot!

Advertisement

Answer

Your request is using sharedWithMe You should be using “owners in”

"'test@example.org' in owners"

from comment

You cant search on files not in a folder (parent) you can only search on files in a folder

'root' in parents will return all the files in root. However 'root' not in parents will fail as its not valid.

Im 100% sure what you mean by parent key. If you only want files in some directories i would suggest that you run requests for each of the directories you want and ignore the ones you dont. This will mean multiple requests or just get everything and sort it locally.

Reference:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement