谷歌驱动器文件上传并将其共享给公共访问

问题描述 投票:1回答:1

我上传文件到谷歌驱动器帐户,我想做的是使其可读,但不能向公众编辑。我怎样才能做到这一点?

$file = new Google_Service_Drive_DriveFile();
$file->title = $_POST['name'];
$file->shared = "public"; //i need to do this
$file->folder = "/somefolder/"; // and this

[可选的]

它实际上是三合一问题:我还想将该文件放入驱动器上的特定文件夹中,我想知道如何在谷歌驱动器上创建该文件夹。

php file-upload google-drive-sdk public google-api-php-client
1个回答
0
投票

可能对新手有用:


<?php
$client = new Google_Client();

// SET Client ID, Client Secret, Tokens as per your mechanism
?>

步骤1:

  1. 创建文件夹。 API将返回创建的文件夹的ID。

第2步:

使用setParents([]);将文件上传到该文件夹

例:

<?php
// Upload the file to efgh folder which is inside abcd folder which is in the root folder
$file = new Google_Service_Drive_DriveFile();
$file->setParents(["abcd123", "efgh456"]); // abcd123 and efgh456 are IDs returned by API for the respective folders
$service = new Google_Service_Drive($client);
$service->files->create($file);
?>
$fileID = ""; // file ID returned by Drive.

第3步:

将权限设置为可共享:

<?php
$permissionService = new Google_Service_Drive_Permission();
$permissionService->role = "reader";
$permissionService->type = "anyone"; // anyone with the link can view the file
$service->permissions->create($fileID, $permissionService);
?>

参考:https://developers.google.com/drive/api/v3/reference/permissions/create

© www.soinside.com 2019 - 2024. All rights reserved.