1 回答

TA貢獻1878條經驗 獲得超4個贊
您想使用 google/apiclient 和 php 將文件從 Google Drive 下載到特定目錄。
您要下載的文件是您的和/或與您共享的。
如果我的理解是正確的,這個修改怎么樣?
修改點:
請getBody()用于$content.
$content->getBody()->eof()
$content->getBody()->read(1024)
修改后的腳本:
在此修改中,Drive API 也檢索文件名并用于保存下載的文件。如果您不想使用它,請刪除它的腳本。
$fileId = "###"; // Please set the file ID.
// Retrieve filename.
$file = $service->files->get($fileId);
$fileName = $file->getName();
// Download a file.
$content = $service->files->get($fileId, array("alt" => "media"));
$handle = fopen("./download/".$fileName, "w+"); // Modified
while (!$content->getBody()->eof()) { // Modified
fwrite($handle, $content->getBody()->read(1024)); // Modified
}
fclose($handle);
echo "success";
在上面的腳本中,下載的文件被保存到./download/.
筆記:
當Drive API的Files: get方法時,可以下載除Google Docs(Google Spreadsheet、Google Document、Google Slides等)以外的文件。如果您要下載 Google Docs,請使用 Files:export 的方法。請注意這一點。
所以在上面修改過的腳本中,它假設您正在嘗試下載除 Google Docs 之外的文件。
- 1 回答
- 0 關注
- 171 瀏覽
添加回答
舉報