Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在google drive v3 PHP中更新文件_Php_Google Api_Google Drive Api_Google Api Php Client - Fatal编程技术网

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

如何在google drive v3 PHP中更新文件,php,google-api,google-drive-api,google-api-php-client,Php,Google Api,Google Drive Api,Google Api Php Client,我似乎不能用下面的代码更新google drive中的文件,一切正常,但文件保持不变?我正在使用v3api function updateFile($service, $fileId, $data) { try { $emptyFile = new Google_Service_Drive_DriveFile(); $file = $service->files->get($fileId); $

我似乎不能用下面的代码更新google drive中的文件,一切正常,但文件保持不变?我正在使用v3api

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

我设法做到了,你必须把空文件作为第二个参数,不知道为什么,但这篇文章帮了我很多忙:

这是最终的解决方案:

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是您正在更新的文件,数据是您正在更新文件的新内容

别忘了在这之后刷新谷歌硬盘,因为它的预览没有改变,我在上面浪费了一个小时:/。希望这有帮助

 function updateFile($fileId,$newDescription){
        try {    
            // First retrieve the file from the API. 
            $emptyFile = new Google_Service_Drive_DriveFile();
            // File's 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
如果您希望更新人员(如描述),该方法是必不可少的。我从v2复制了这个想法

// File's new metadata.
$file->setTitle($newTitle);
$file->setDescription($newDescription);
$file->setMimeType($newMimeType);
NB:您还必须确保更新函数的3个参数是一个数组
也如其他有用答案中所述;请确保刷新google drive以检查

您是否可以为此提供完整的请求和响应?您是否尝试过其他不同的文件类型(如google文档)?这样做有效吗?非常感谢您提到Google Drive预览不会刷新。你刚才救了我一个小时。fistbumpYo数据参数exaclty是什么,它只是一个字符串还是某种JSON?