1

I've found a lot of topics about this here in StackOverflow but it's outdated some are 8 years ago.

I am displaying a table tree using Google Drive API & PHP and already did it but it's so slow, taking sometimes 30 seconds to load.

Here's my code:

$root['pageSize'] = 10;
$root['q'] = "mimeType='application/vnd.google-apps.folder' and name='MainFolder' and trashed=false";
$root['fields'] = 'files(id, name, modifiedTime, mimeType), nextPageToken'; 
$rootfolder = $service->files->listFiles($root);
    foreach( $rootfolder as $pp => $mainroot) {
                            
$parameters['pageSize'] = 10;
$parameters['q'] = "'{$mainroot['id']}' in parents"; 
$parameters['fields'] = 'files(id, name,modifiedTime), nextPageToken';
    
$files = $service->files->listFiles($parameters);

foreach( $files as $k => $file ){
   echo " <tr>
                <td><i class='ti-folder'></i> 
                <button class='toggleDetails'> "; 
                    echo $file['name'];
                echo "</button>";
            
                // display subfolders
                    $parameters2['pageSize'] = 10;
                    $parameters2['q'] = "'{$file['id']}' in parents";
                    $parameters2['fields'] = 'files(id, name, modifiedTime, mimeType), nextPageToken'; 
                    $sub_files = $service->files->listFiles($parameters2);
                        echo " <ul class='details filelistdown'>    ";
                        foreach( $sub_files as $kk => $sub_file ) {
                            echo " <li>&nbsp&nbsp&nbsp&nbsp&nbsp<i class='ti-calendar'></i>&nbsp{$sub_file['name']}</li>";  
                                
                                // list files
                                $subfiles_2['pageSize'] = 10;
                                $subfiles_2['q'] = "'{$sub_file['id']}' in parents";
                                $subfiles_2['fields'] = 'files(id, name, modifiedTime, mimeType, webViewLink, webContentLink), nextPageToken'; 
                                $sub_files_two_2 = $service->files->listFiles($subfiles_2);
                                    echo "<ul>";
                                        foreach( $sub_files_two_2 as $uu => $sub_file_2 ) {
                                            echo "<li> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp *{$sub_file_2['name']} -- <a href='{$sub_file_2['webContentLink']}' target='_blank' class='badge badge-success'>Download</a> or <a href='{$sub_file_2['webViewLink']}' target='_blank' class='badge badge-primary'>View</a></li>";
                                        }
                                    
                            echo "</ul>";                                   
                    }
                }
            }
        

I just want to ask if there's any way to speed up to load files?

Thanks

4
  • Why are you setting pageSize to 10? Set it to the maximum instead, 1000. Also, I'd first retrieve all files in the Drive, and then organize them according to their parents. There's no need to recursively call the API, as others said.
    – Iamblichus
    Jun 8, 2021 at 7:12
  • @Iamblichus hello sir. I set it to 10 because I thought it may help on optimizing the loading. I'd first retrieve all files in the Drive, and then organize them according to their parents. That's what I did sir using '$idparent' in parents. Any recommendation how to call their childrens? I'm using v3 btw
    – Mon Padi
    Jun 8, 2021 at 7:23
  • No, the thing is you should not call $parameters['q'] = "'{$mainroot['id']}' in parents"; if you want to get all files (and not just files which are not in any subfolder). The thing is (1) get all files, (2) based on the files' parents, build up the tree structure locally, without calling the API anymore (since you already got all the information necessary). Does that make sense to you? Do you need more assistance with that?
    – Iamblichus
    Jun 8, 2021 at 9:14
  • @Iamblichus Hello sir. I think it makes sense of what you are suggesting. But I need some help on how will I build up using PHP for I am still trying to learn.
    – Mon Padi
    Jul 16, 2021 at 5:33

1 Answer 1

1

Google drive API is a free API you get what you pay for, I wouldn't try to focus to much on speed you will then just be hit by quota exceptions.

With regard to your code you are making to many requests. If your after directory structure you could just run one full request load everything into a single variable and then sort it locally on your own machine. Seeing as max page size for the file list method is 1000 you could cut down the number of requests by a lot by not having to making a call for each directory to get its children.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.