如何在google drive v3 PHP中更新文件

Map*_*rup 8 php google-api google-drive-api google-api-php-client

我似乎无法使用以下代码更新谷歌驱动器中的文件,一切正常但文件保持不变?我正在使用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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Map*_*rup 15

我设法做到了,你必须把空文件作为第二个参数,不知道为什么,但这篇文章对我帮助很大:Google Drive API v3 Migration

这是最终解决方案:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

其中$ fileId是您要更新的文件,数据是您正在更新文件的新内容.

不要忘记在此之后刷新谷歌驱动器,因为它的预览没有改变,我失去了一个小时:/.希望这可以帮助.

  • 非常感谢您提及Google Drive预览无法刷新.那小时你就救了我.*fistbump* (2认同)