Rg14 Rg14 -4 years ago 147
PHP Question

Dynamically Create Folders and Subfolders

I have several scripts in php to create a folder and upload a file to the desired folder, but I can't seem to be able to create new folders, new subfolders or create another function to do this process. Do i have to use a parent/children specific procedure? Here's the functional code I use to create a folder:

function getFolderExistsCreate($service, $folderName, $folderDesc) {
// Lista el root de drive
$files = $service->files->listFiles();
$found = false;

// Checa los folders en ciclo
foreach ($files['items'] as $item) {
if ($item['title'] == $folderName) {
$found = true;
return $item['id'];

// Lo crea si no lo encontro
if ($found == false) {
$folder = new Google_Service_Drive_DriveFile();

//Poner los settings del folder



//Try de crear el folder
try {
$createdFile = $service->files->insert($folder, array(
'mimeType' => 'application/vnd.google-apps.folder',

// Regresa el nuevo id del folder
return $createdFile->id;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();

Help please, thanks!

Answer Source

To create a subfolder to a particular folder, you should specify the correct ID in the parents property of the file.

"parents": [{"id":"0ADK06pfg"}]

Check this page for more information

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download