I have been struggling for quite a while now; via Google drives PHP API, I am able to create a sub folder or add files to an existing folder, But trying to place another sub folder or a file within a sub folder, seems impossible.
After research, I came across the Children function, but don't understand how to apply it, even after checking the Google documentation on this page: [https://developers.google.com/drive/v2/reference/children/insert][1]
The code I am using to add an image to a folder is:
//Insert a file into client specific folder
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('a.jpg');
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($folderid); //$folderid = determined folder id
$file->setParents(array($parent));
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
How would I proceed to upload this image to a specific subfolder, of which the ID is already determined?
Update: This is my combined code trying to:
- Create new_sub_folder1 with a parent of existing_folder, and store the returned ID
- Create new_file1 with a parent of new_sub_folder1, using the ID stored in step 1
$service = new Google_Service_Drive($client);
//create sub folder
$folder = new Google_Service_Drive_DriveFile();
//Setup the folder to create
$folder->setTitle('new_sub_folder1');
$folder->setMimeType('application/vnd.google-apps.folder');
//Create the Folder within existing_folder
$parentid = '0B40CySVsd_Jaa1BzVUQwLUFyODA';
//Set the Parent Folder to existing_folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($parentid);
$folder->setParents(array($parent));
//now create the client specific folder new_sub_folder
try {
$createdFile = $service->files->insert($folder, array(
'mimeType' => 'application/vnd.google-apps.folder',
));
// Return the created folder's id
$subfolderid = $createdFile->id;
echo $subfolderid;
return $createdFile->id;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
//Insert a file into client specific folder new_sub_folder
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('a.jpg');
//Set the Parent Folder to new_sub_folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($subfolderid);
$file->setParents(array($parent));
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
print_r($createdFile);
But only new_sub_folder1 is created, and no file is added to this folder.
Update: The image is not added anywhere with this code. If I apply the same method to add .jpg to the existing_folder, by its ID, there are no issues. As soon as I use the sub_folder_1's ID, nothing is created - same method, different ID.
Think of the folders as a train. Where your file (in this case a folder) has to be attached to 2 other train carts in order to work. The previous one is the parent and the next one is the child. If you specify a parent, but not a child for a folder then the train is incomplete. Sometimes it doesn't matter that the train is not complete, but if you want to use at least 2 levels of files then you must use parent and children references.
Your base folder may have root as its parent, then this folder could be the father of another sub-folder but it needs to be specified that it has children if you plan to use several level hierarchy for it to have a complete reference. So check this code:
A few things about the code: foldID is my kind of master or root folder. I create a new DriveFile, assign the folder specifications, assign a parent reference of my master folder, try to insert it, then specify a child reference with the children->insert(ID OF MY MASTER FOLDER, ID OF THE CREATED FOLDER). The reference now is complete, and the subfolder should be treated as such. For another sub-sub-folder, just rinse and repeat with the right parameters. I have a directory structure of approximately 6 or 7 level of folders and files, so it is possible so it's just to index the files correctly.