How to list sub and shared folders by using Google Drive API with a PHP client?

Farid Movsumov
5 min readSep 3, 2023

Allowing users to fetch all folder names from Google Drive, including sub-folders and shared folders, and display them meaningfully to the end user is a complex task requiring you to think about many details in your code. I have been working on a web app in production for over two years, where I faced a lot of edge cases, and I would like to share my experience in this post.

Okay, let’s first see how to display folders to the end user. For simplicity, I decided to show folders in a drop-down (select box).

An important detail here is to specify shared folders and display child folders so that the end user will understand it is a sub-folder.

Specifying a shared folder is easy. You append the “(shared)” text to the end of the folder name.

We are adding parent folder names as a prefix and separate folder names with some special characters for subfolders. I choose “>>” in my case you can choose something different.

You can see the real example screenshot below.

Okay, now we know what we want to achieve, let’s dive into the code and see how we can achieve this.

We will need to implement 3 methods. The first method is the getFolders method. Here, we accept the Shop…