如何将文件上传到谷歌驱动器中的特定文件夹 - Google 驱动器 API (PHP)

Shi*_*lat 5 php google-drive-api

如何将文件上传到 google drive -Google drive API 中的特定文件夹?我有这个代码,它将我的文件上传到谷歌驱动器中的主文件夹。

$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
    $file_path = 'files/'.$file_name;
    $mime_type = finfo_file($finfo, $file_path);
    $file->setTitle($file_name);
    $file->setDescription('This is a '.$mime_type.' document');
    $file->setMimeType($mime_type);
    $service->files->insert($file, array(
        'data' => file_get_contents($file_path),
         'mimeType' => $mime_type
    ));
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*air 1

以下是在 V3 中的操作方法。这称为“设置父级”,因为一旦文件位于文件夹内,它还将继承其所在父级的共享选项。

下面两行将设置文件名,然后setParents将定义要将文件放入哪个文件夹。参数是一个数组,但不要放入超过 1 项,否则会出现错误:

$file->setName("world");
$file->setParents(['SOME_UNIQUE_FOLDER_ID']);
Run Code Online (Sandbox Code Playgroud)