第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何使用php將谷歌驅(qū)動(dòng)下載文件寫入目錄

如何使用php將谷歌驅(qū)動(dòng)下載文件寫入目錄

PHP
慕碼人8056858 2021-11-19 16:26:06
我正在嘗試使用下面的代碼將谷歌驅(qū)動(dòng)器文件下載到目錄。當(dāng)我運(yùn)行代碼時(shí),它只按照下面的代碼在瀏覽器上打開文件的內(nèi)容// 驗(yàn)證谷歌驅(qū)動(dòng)器到這里$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();這是我如何嘗試將其下載到名為目標(biāo)的目錄// 打開文件句柄輸出。$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";這是我得到的錯(cuò)誤致命錯(cuò)誤:未捕獲的錯(cuò)誤:調(diào)用 C:\xampp\htdocs\download.php 中字符串的成員函數(shù) eof()
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

您想使用 google/apiclient 和 php 將文件從 Google Drive 下載到特定目錄。

您要下載的文件是您的和/或與您共享的。

如果我的理解是正確的,這個(gè)修改怎么樣?


修改點(diǎn):

請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/.

筆記:

當(dāng)Drive API的Files: get方法時(shí),可以下載除Google Docs(Google Spreadsheet、Google Document、Google Slides等)以外的文件。如果您要下載 Google Docs,請使用 Files:export 的方法。請注意這一點(diǎn)。

所以在上面修改過的腳本中,它假設(shè)您正在嘗試下載除 Google Docs 之外的文件。


查看完整回答
反對 回復(fù) 2021-11-19
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號