How to update file in Google Drive v3 PHP

I can’t update the file on Google Drive with the following code, everything is going fine, but the file remains untouched? I work 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(); } } 
+8
php google-drive-sdk google-api google-api-php-client
source share
2 answers

I managed to do this, you should put an empty file as the second argument, you don’t know why, but this post helped me a lot: Google Drive API v3 Migration

This is the 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 the file that you are updating, and data is the new content that you are updating.

Do not forget to update Google Drive after that, because its viewing does not change, and I lost one hour: /. Hope this helps.

+15
source share
  function updateFile($fileId,$newDescription){ try { // First retrieve the file from the API. $emptyFile = new Google_Service_Drive_DriveFile(); // File new metadata. $emptyFile->setDescription($newDescription); // Send the request to the API. $driveService->files->update($fileId, $emptyFile, array()); print 'success'; } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); } }//end update 

This method is necessary if you want to update staff as a description. I copied the idea from v2.

 // File new metadata. $file->setTitle($newTitle); $file->setDescription($newDescription); $file->setMimeType($newMimeType); 

NB: You also need to make sure that 3 parameters of the update function is an array

0
source share

All Articles