Jazuly
Jazuly

Reputation: 1414

how to check if file trashed or not google drive api

i have a problem, im using this client form my app to google drive api.

but the problem is i cant check if the files is trashed or not.

here what i try

$client = new \Google_Client();
$client->setScopes(\Google_Service_Drive::DRIVE);
$client->setAuthConfig(app_path().'/Credentials/gDrive.json');

$service = new \Google_Service_Drive($client);
$folder = $service->files->get("fileid");

this always return trashed as null, what ever it trashed or not..

Upvotes: 2

Views: 1699

Answers (1)

Tanaike
Tanaike

Reputation: 201603

I believe your goal and your current situation as follows.

  • You want to check whether whether the file is put in "Trash" using googleapis for php.
  • You have already been able to get the file metadata using Drive API.
  • You are using Drive API v3.

Modification point:

  • When Drive API v3 is used, all file metadata is not returned by the method of "Files: get". I think that the reason of your issue is this. So in this case, it is required to use fields. In your case, if you want to check only trashed, you can use trashed as the fields.

When this is reflected to your script, it becomes as follows.

Modified script:

From:
$folder = $service->files->get("fileid");
To:
$folder = $service->files->get("fileid", array("fields" => "trashed"));
$trashed = $folder['trashed'] ? "true" : "false";
echo $trashed;
  • When the file of fileid is put in "Trash", $trashed is true.
  • When the file of fileid is NOT put in "Trash", $trashed is false.

Note:

  • For example, you want to all file metadata, you can use "fields" => "*" instead of "fields" => "trashed".

Reference:

Upvotes: 2

Related Questions