使用PHP和Drive API将文件上传到Google云端硬盘上的特定文件夹

Vla*_*šez 4 php google-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>';

?>
Run Code Online (Sandbox Code Playgroud)

为了将此文件上传到用户驱动器上的特定文件夹,我该怎么做?

谢谢

rai*_*ace 7

我猜这很疯狂,但谷歌又改变了API!ParentReference类现在是Google_Service_Drive_ParentReference

所以更新的代码是:

$parent = new Google_Service_Drive_ParentReference();
$parent->setId($folderId);
$file->setParents(array($parent));
Run Code Online (Sandbox Code Playgroud)


Vla*_*šez 5

谢谢你的回答.是的,这是解决方案,我之前在API中找到了它,但它没有用.

关键是谷歌改变了类名,但没有更新文档.现在是ParentReference类Google_ParentReference()

与其他有关Google云端硬盘的文档存在类似问题.所以,这个的工作代码是:

$parent = new Google_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
Run Code Online (Sandbox Code Playgroud)