亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用php將谷歌驅動下載文件寫入目錄

如何使用php將谷歌驅動下載文件寫入目錄

PHP
慕碼人8056858 2021-11-19 16:26:06
我正在嘗試使用下面的代碼將谷歌驅動器文件下載到目錄。當我運行代碼時,它只按照下面的代碼在瀏覽器上打開文件的內容// 驗證谷歌驅動器到這里$file = $service->files->get($fileId);  $downloadUrl = $file->getDownloadUrl();    $request = new Google_Http_Request($downloadUrl, 'GET', null, null);    $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);   echo $content= $httpRequest->getResponseBody();這是我如何嘗試將其下載到名為目標的目錄// 打開文件句柄輸出。$content = $service->files->get($fileId, array("alt" => "media"));// Open file handle for output.$handle = fopen("/download", "w+");// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.while (!$content->eof()) {        fwrite($handle, $content->read(1024));}fclose($handle);echo "success";這是我得到的錯誤致命錯誤:未捕獲的錯誤:調用 C:\xampp\htdocs\download.php 中字符串的成員函數 eof()
查看完整描述

1 回答

?
UYOU

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 之外的文件。


查看完整回答
反對 回復 2021-11-19
  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號