[英]Upload file into particular folder on Google Drive using PHP and Drive API
希望这是一个简单的...我想直接将文件上传到文件夹中。 我有文件夹ID,我有这个代码完全上传根文件:
<?php
require_once 'get_access.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$folder_id = '<folder id>';
$client = new Google_Client();
$client->setClientId('<client_id>');
$client->setClientSecret('<client_secret>');
$client->setRedirectUri('<url>');
$client->setScopes(array('<scope>'));
$client->setAccessToken($access);
$client->setAccessType('offline');
$client->refreshToken($refreshToken);
$service = new Google_DriveService($client);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document 123');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document-2.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
echo '<pre>';
var_dump($createdFile);
echo '</pre>';
?>
为了将此文件上传到用户驱动器上的特定文件夹,我该怎么做?
谢谢
我猜这很疯狂,但谷歌又改变了API! ParentReference类现在是Google_Service_Drive_ParentReference
所以更新的代码是:
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($folderId);
$file->setParents(array($parent));
感谢您的回答。 是的,这是解决方案,我之前在API中找到了它,但它没有用。
关键是谷歌改变了类名,但没有更新文档。 ParentReference类现在是Google_ParentReference()
与其他有关Google云端硬盘的文档存在类似问题。 所以,这个的工作代码是:
$parent = new Google_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
假设$parentId
包含目标文件夹的id,您应该在插入请求之前添加此代码:
$parent = new ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
有关更多详细信息,请查看files.insert
参考指南:
https://developers.google.com/drive/v2/reference/files/insert
很晚了,但是我到达了这个页面寻找解决方案,并且由于API改为V3,程序也发生了变化。 几个小时后我发现了它。
以新的方式,他们使它变得非常简单。
尝试这个:
$file = new Google_DriveFile();
$file->setName('YourDocument.txt'); // This also changed from Title To Name
$file->setParents(array($parentId)); // This is the Line which sets parent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.