如何使用php将google drive下载文件写入目录
我正在尝试使用下面的代码将google驱动器文件下载到一个目录。当我运行代码时,它只会按照下面的代码在浏览器上打开文件的内容 //谷歌硬盘的认证就在这里如何使用php将google drive下载文件写入目录,php,google-drive-api,Php,Google Drive Api,我正在尝试使用下面的代码将google驱动器文件下载到一个目录。当我运行代码时,它只会按照下面的代码在浏览器上打开文件的内容 //谷歌硬盘的认证就在这里 $file = $service->files->get($fileId); $downloadUrl = $file->getDownloadUrl(); $request = new Google_Http_Request($downloadUrl, 'GET', null, null); $htt
$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();
下面是我如何将它下载到一个名为destinations的目录
//打开文件句柄进行输出
$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()
- 请对
使用$content
。getBody()
$content->getBody()->eof()
$content->getBody()->读取(1024)
$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/
- 当文件的方法:获取驱动API时,可以下载除Google文档(Google电子表格、Google文档、Google幻灯片等)以外的文件。如果你想下载谷歌文档,请使用文件的方式:导出。请小心这个。
- 所以在上面修改的脚本中,它假设您正在尝试下载除Google文档之外的文件
如果我误解了你的问题,而这不是你想要的结果,我道歉。@jmarkatti我很高兴你的问题得到了解决。也谢谢你。