如何更新谷歌驱动器v3 PHP中的文件 [英] How to update file in google drive v3 PHP

查看:106
本文介绍了如何更新谷歌驱动器v3 PHP中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用下面的代码更新谷歌驱动器中的文件,一切正常,但文件保持不变?我正在使用v3 api。
$ b

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();
        }
    }


推荐答案

I设法做到这一点,你必须把空文件作为第二个参数,不知道为什么,但这篇文章帮了我很多: Google Drive API v3迁移

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();
        }
    }

其中$ fileId是您要更新的文件,是你正在更新你的文件的新内容。

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.

这篇关于如何更新谷歌驱动器v3 PHP中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆