Creating directories in Google Drive

Hello,
The code below creates a directory, but what should I do to create subdirectories?

Example
$dir = ‘aaa/bbb/ccc/ddd’;

    function create_dir_or_subdir($service, $parentId, $subdirectory) {
        $file = new Google_Service_Drive_DriveFile();
        $file->setName($subdirectory);
        $file->setMimeType('application/vnd.google-apps.folder');
        $parentId = !empty($parentId) ? $parentId : null;
        $file->setParents(array($parentId));

        //create the ProjectFolder in the Parent
        try {
            $createdFile = $service->files->create($file, array(
            'mimeType' => 'application/vnd.google-apps.folder',
        ));
            // print_r($createdFile);
            return $createdFile->id;
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
    }

To create subdirectories within Google Drive using the Google Drive API and PHP, you would need to recursively create each folder one by one, setting the parent of each subsequent folder to the folder that was just created. Here’s how you could modify your function to create a structure like aaa/bbb/ccc/ddd:

function createDirectoryPath($service, $path, $parentId = null) {
    $directories = explode('/', $path);
    foreach ($directories as $directory) {
        $parentId = createSubdirectory($service, $parentId, $directory);
    }
    return $parentId; // The ID of the last folder created
}

function createSubdirectory($service, $parentId, $subdirectoryName) {
    $fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => $subdirectoryName,
        'mimeType' => 'application/vnd.google-apps.folder',
        'parents' => $parentId ? array($parentId) : null
    ));

    try {
        $folder = $service->files->create($fileMetadata, array('fields' => 'id'));
        printf("Folder ID: %s\n", $folder->id);
        return $folder->id; // Return the ID of the created folder
    } catch (Exception $e) {
        echo "An error occurred: " . $e->getMessage();
    }
    return null; // Return null if something went wrong
}

// Usage:
$service = ...; // Assume you have a $service instance for Google Drive API
$fullPath = 'aaa/bbb/ccc/ddd';
$rootId = null; // Set this if you want to create the structure in a specific folder

$lastFolderId = createDirectoryPath($service, $fullPath, $rootId);

This code splits the desired path into its components, creates each directory if it doesn’t exist, and sets the parent for each subsequent directory. It returns the ID of the last folder created, which would be ddd in your example. If you want to create this structure within a specific existing folder, you can pass that folder’s ID as the $rootId.

Good luck

1 Like

Thank you very much, the result is in the picture below

Ekran görüntüsü 2023-11-05 223940

It didn’t turn out exactly the way I wanted, but when things get there, I will ask for your help again.

Folders files tree in service account in Google Drive

According to your video, each file is put where you want it to be, I do not see any errors. You only need a logical way to align your files as you want them to be.

Am glad that I help you in achieving your aspect.

On the local computer, FTP and Google Drive are very slow. Probably because it’s local
You helped me a lot, thank you very much
No verification with Google Service account, no return url issues, this is nicer :grinning: :grinning: :grinning: :+1: :+1:

You are welcome :smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service