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.