I cant seem to update file in google drive with the following code, everything goes fine but file remains untouched? I am working with v3 api.
function updateFile($service, $fileId, $data) {
try {
$emptyFile = new Google_Service_Drive_DriveFile();
$file = $service->files->get($fileId);
$service->files->update($fileId, $emptyFile, array(
'data' => $data,
'mimeType' => 'text/csv',
'uploadType' => 'multipart'
));
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
Google Drive API PHP Library To make the process simple, we will build a custom library to handle the Google Drive API calls with PHP. Our Google API Library helps to authenticate with Google account and access the Drive API with REST API using PHP cURL.
Right-click the file, choose Manage Versions from the menu and then click the Upload new version button to upload the updated file to your Google Drive. That's it. The file name and the file's URL won't change and thus all the old shared links will now automatically point to the new version of your file.
I managed to do it, you have to put empty file as second argument, not sure why but this post helped me a lot: Google Drive API v3 Migration
This is final solution:
function updateFile($service, $fileId, $data) {
try {
$emptyFile = new Google_Service_Drive_DriveFile();
$service->files->update($fileId, $emptyFile, array(
'data' => $data,
'mimeType' => 'text/csv',
'uploadType' => 'multipart'
));
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
where $fileId is file you are updating, and data is new content you are updating your file.
Dont forget to refresh google drive after this because it's preview doesnt change and I lost one hour on that :/. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With