gpt4 book ai didi

php - 使用 PHP 和 Drive API 将文件上传到 Google Drive 上的特定文件夹

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:56:49 25 4
gpt4 key购买 nike

希望这是一个简单的...我想将文件直接上传到文件夹中。我得到了文件夹 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>';

?>

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

谢谢

最佳答案

我想这太疯狂了,但 Google 又一次更改了 API! ParentReference 类现在是 Google_Service_Drive_ParentReference

所以更新后的代码是:

$parent = new Google_Service_Drive_ParentReference();
$parent->setId($folderId);
$file->setParents(array($parent));

关于php - 使用 PHP 和 Drive API 将文件上传到 Google Drive 上的特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14566325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com