Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update file in google drive v3 PHP

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();
        }
    }
like image 557
MapleSyrup Avatar asked Feb 09 '16 15:02

MapleSyrup


People also ask

Does Google Drive support PHP?

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.

How do I update Google Drive with the same link?

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.


1 Answers

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.

like image 76
MapleSyrup Avatar answered Oct 19 '22 04:10

MapleSyrup