2015-02-18 3 views
0

Я пытаюсь обновить содержимое файла. Используйте функцию PHP:Google API PHP Обновление файла

function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) { 
    try { 
     // First retrieve the file from the API. 
     $file = $service->files->get($fileId); 

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

     // File's new content. 
     $data = file_get_contents($newFileName); 

     $additionalParams = array(
      'newRevision' => $newRevision, 
      'data' => $data, 
      'mimeType' => $newMimeType 
     ); 

     // Send the request to the API. 
     $updatedFile = $service->files->update($fileId, $file, $additionalParams); 
     return $updatedFile; 
    } catch (Exception $e) { 
     print "An error occurred: " . $e->getMessage(); 
    } 
} 

.... 
$data = retrieveAllFiles($service); 


$fileName = 'test.txt'; 
$mimeType = mime_content_type('./'.$fileName); 


$res = updateFile($service, $data[0]['id'], $data[0]['title'], 'update', $mimeType, $fileName, true); 

Я пытаюсь добавить строку текстового файла «тестовая строка». Функция обновляет файл данных (описание, lastModifyingUser ...), но содержимое файла остается неизменным. Кто может сказать, что случилось?

+0

Попробуйте отправить тот же запрос в API-обозреватель и посмотреть, сможете ли вы правильно увидеть обновление. Вот ссылка для API-обозревателя: https://developers.google.com/apis-explorer/#p/ – KRR

ответ

0

В additionalParams нужно добавить:

'uploadType' => 'multipart', 
or 
'uploadType' => 'media', 

Надеется, что это помогает!

Смежные вопросы