[英]Google drive PHP API: unable to insert files or folders into subfolders
我已经苦苦挣扎了好一阵子。 通过Google驱动器PHP API,我可以创建一个子文件夹或将文件添加到现有文件夹,但是尝试将另一个子文件夹或一个文件放在一个子文件夹中似乎是不可能的。
经过研究,我遇到了儿童功能,但是即使查看了此页上的Google文档,也无法理解如何使用该功能:[ https://developers.google.com/drive/v2/reference/children/insert ] [1]
我用来将图像添加到文件夹的代码是:
//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'
));
我将如何继续将此图像上传到已确定ID的特定子文件夹?
更新:这是我的组合代码,试图:
- 创建一个带有现存文件夹父级的new_sub_folder1并存储返回的ID
- 使用步骤1中存储的ID,使用new_sub_folder1的父项创建new_file1
$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);
但是仅创建了new_sub_folder1,并且没有文件添加到该文件夹。
更新:此代码未将图像添加到任何地方。 如果我使用相同的方法通过其ID将.jpg添加到existing_folder,则没有问题。 一旦我使用sub_folder_1的ID,就不会创建任何内容-相同的方法,不同的ID。
我的猜测是您误解了文件夹在云端硬盘中的工作方式。 在云端硬盘中,重要的是将文件夹视为标签。
您说:“我无法:existing_folder-> new_sub_folder1-> new_file1,new_file2”
在云端硬盘中,这很简单:
将文件夹视为一列火车。 您的文件(在这种情况下为文件夹)必须附加到其他2个火车车厢上才能工作。 前一个是父母,下一个是孩子。 如果您为文件夹指定父项,而不是子项,则说明火车不完整。 有时,训练没有完成并不重要,但是如果要使用至少2个级别的文件,则必须使用父级和子级引用。
您的基本文件夹可能以root作为其父文件夹,然后该文件夹可能是另一个子文件夹的父亲,但是如果您打算使用多个级别的层次结构来拥有完整的引用,则需要指定该文件夹具有子文件夹。 因此,请检查以下代码:
$foldID=$id;
$folder4=new Google_Service_Drive_DriveFile();
$folder4->setTitle($folderName);
$folder4->setMimeType('application/vnd.google-apps.folder');
$parent4=new Google_Service_Drive_ParentReference();
$parent4->setId($foldID);
$folder4->setParents(array($parent4));
try {
$createdFile4 = $service->files->insert($folder4, array(
'mimeType' => 'application/vnd.google-apps.folder',
));
}
catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
$foldId4=$createdFile4->id;
$newChild4=new Google_Service_Drive_ChildReference();
$newChild4->setId($createdFile4->id);
try{
$service->children->insert($foldID,$newChild4);
}
catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
有关代码的几件事:foldID是我的主文件夹或根文件夹。 我创建一个新的DriveFile,分配文件夹规格,分配主文件夹的父引用,尝试插入它,然后使用children-> insert(ID MASTER FOLDER,ID OF CREATED FOLDER)指定子引用。 现在参考已完成,子文件夹也应这样处理。 对于另一个子文件夹,只需冲洗并以正确的参数重复即可。 我的目录结构大约有6或7个级别的文件夹和文件,因此有可能只是为了正确索引文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.