繁体   English   中英

如何使用 php 删除 google drive 共享驱动器文件夹中的文件?

[英]How to delete a file in a google drive shared drive folder using php?

我只需要使用 php 使用 google/apiclient 删除我的共享驱动器文件夹中的文件,这是我下面的代码。

session_start();

require __DIR__ . '/vendor/autoload.php'; // ready the API to upload to drive

use Google\Client;
use Google\Service\Drive;

if (isset($_POST['file'])) {
    $file = $_POST['file'];

        $client = new Client();
        putenv('GOOGLE_APPLICATION_CREDENTIALS=./credentials.json');
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);

        $delete = $driveService->files->delete($file);

        if ($delete) {
            $_SESSION['success'] = "Video deleted successfully";
            header("Location: upload");
        }

}



如果你的客户端有删除共享Drive文件的权限,下面的修改怎么样?

从:

$delete = $driveService->files->delete($file);

到:

$fileId = "###"; // Please set the file ID of the file you want to delete.
try {
    $driveService->files->delete($fileId, array('supportsAllDrives' => true));
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
  • 在这种情况下,删除文件时,不会返回任何值。 请注意这一点。

笔记:

  • 当我测试这个脚本时,我确认可以删除共享驱动器中的文件。 但是,如果发生错误,请再次确认您的客户的许可。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM