我一直在尝试使用google api创建第一个根文件夹,然后在该根文件夹中创建子文件夹。然而,结果并不令人鼓舞。以下是创建文件夹的代码:
public function createFolder($name, $parents = array())
{
/** @var $client \Google_Client */
$client = $this->client;
/** @var $service \Google_Service_Drive */
$service = $this->service;
if (is_null($service)) {
throw new Exception("Invalid google api service");
}
if (!$client instanceof \Google_Client) {
throw new Exception("Invalid google api client");
}
if (!isset($_SESSION['access_token'])) {
throw new Exception("No access token found");
}
if (!$client->getAccessToken()) {
throw new Exception("Could not find access token in client");
}
$folder = new \Google_Service_Drive_DriveFile();
$folder->setTitle($name);
$folder->setMimeType('application/vnd.google-apps.folder');
if (!empty($parents)) {
echo "Setting parents of $name to: " . implode(',', $parents);
$folder->setParents($parents);
} else {
echo "\n No parent ids found \n";
}
echo "\n Creating new folder: {$name} \n";
$output = $service->files->insert($folder, array(
'mimeType' => 'application/vnd.google-apps.folder',
));
return $output;
}
然而,每当我运行此脚本时,父母都没有设置。相反,子文件夹位于父文件夹旁边的驱动器中。
此外,我尝试手动设置父标志,如下所示:
public function insertFileToFolder($childId, $parentId)
{
if (empty($childId) || empty($parentId)) {
throw new Exception("Invalid parameter");
}
$service = $this->getService();
try {
echo "\n Trying to set subfolders \n";
$parentReference = new \Google_Service_Drive_ParentReference();
$parentReference->setId($parentId);
return $service->parents->insert($childId, $parentReference);
} catch (Exception $ex) {
echo $ex->getMessage();
}
return null;
}
哪个确实有用......有点儿。它没有移动"父文件夹的子文件夹,而是复制它们或创建某种符号链接,文件夹似乎位于父文件夹下。
编辑:我已经确认insertFileToFolder不会复制文件,而是设置符号链接。从root用户删除文件夹也会从父文件夹中删除它。
真的很简单的问题:我错过了什么?
答案 0 :(得分:1)
在你的函数'insertFileToFolder'中,不使用parents->插入函数,而是使用children-> insert('FolderID','newChild')
您可以在此链接上看到确切的实施:https://developers.google.com/drive/v2/reference/children/insert#examples