Php 谷歌硬盘文件上传并共享给公众访问

Php 谷歌硬盘文件上传并共享给公众访问,php,file-upload,google-drive-api,public,google-api-php-client,Php,File Upload,Google Drive Api,Public,Google Api Php Client,我上传文件到谷歌驱动器帐户,我想做的是让它可读,但不可编辑的公众。我该怎么做 $file = new Google_Service_Drive_DriveFile(); $file->title = $_POST['name']; $file->shared = "public"; //i need to do this $file->folder = "/somefolder/"; // and this [可选] 这实际上是一个三位一体的问题:我还想把那个文件放到驱动器上

我上传文件到谷歌驱动器帐户,我想做的是让它可读,但不可编辑的公众。我该怎么做

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

这实际上是一个三位一体的问题:我还想把那个文件放到驱动器上的一个特定文件夹中,我不知道如何在谷歌驱动器上创建那个文件夹

可能对新手有用:

步骤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);
?>

参考资料:

不确定它是否很好地内置到PHP库中,但您可以通过查看有关权限的常规API文档开始,我猜您需要“type:anywhere”
<?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.
<?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);
?>