【问题标题】:How can i upload file to a specific folder in google drive -Google drive API (PHP)我如何将文件上传到谷歌驱动器中的特定文件夹-谷歌驱动器 API (PHP)
【发布时间】:2018-09-09 07:19:16
【问题描述】:

如何将文件上传到 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
    ));
}

【问题讨论】:

标签: php google-drive-api


【解决方案1】:

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

以下两行将设置文件的名称,然后 setParents 将定义您要将文件放入的文件夹。参数是一个数组,但不要放置超过 1 项或您'会报错:

$file->setName("world");
$file->setParents(['SOME_UNIQUE_FOLDER_ID']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多