简体   繁体   English

如何使用php将Google驱动器下载文件写入目录

[英]how to write a google drive download file to a directory using php

Am trying download a google drive files to a directory using the code below. 我正在尝试使用以下代码将Google驱动器文件下载到目录中。 when I run the code it only open the content of the file on the browser as per code below 当我运行代码时,它仅按照以下代码在浏览器中打开文件的内容

// authentication google drive goes here //身份验证Google驱动器转到此处

$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();

Here is how am trying to download it to a directory called destinations 这是尝试将其下载到名为destinations的目录的方法

// Open file handle for output. //打开文件句柄以进行输出。

$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";

here is the error i got 这是我得到的错误

Fatal error: Uncaught Error: Call to a member function eof() on string in C:\\xampp\\htdocs\\download.php 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ download.php中的字符串上调用成员函数eof()

  • You want to download a file from Google Drive to the specific directory using google/apiclient with php. 您要使用google / apiclient和php将文件从Google云端硬盘下载到特定目录。
  • The files you want to download are yours and/or shared with you. 您要下载的文件是您的文件和/或与您共享的文件。

If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何?

Modification point: 修改点:

  • Please use getBody() for $content . 请对$content使用getBody()
    • $content->getBody()->eof()
    • $content->getBody()->read(1024)

Modified script: 修改后的脚本:

In this modification, the filename is also retrieved by Drive API and used for saving the downloaded file. 在此修改中,文件名也可以通过Drive API检索,并用于保存下载的文件。 If you don't want to use it, please remove the script for it. 如果您不想使用它,请删除它的脚本。

$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";
  • In above script, the downloaded file is saved to the directory of ./download/ . 在上面的脚本中,下载的文件保存到./download/目录。

Note: 注意:

  • When the method of Files: get of Drive API, the files except for Google Docs (Google Spreadsheet, Google Document, Google Slides and so on) can be downloaded. 当使用Drive API的“文件:获取”方法时,可以下载Google Docs(Google电子表格,Google文档,Google幻灯片等)以外的文件。 If you want to download Google Docs, please use the method of Files: export. 如果要下载Google文档,请使用“文件:导出”方法。 Please be careful this. 请注意这一点。
    • So in above modified script, it supposes that you are trying to download the files except for Google Docs. 因此,在上面修改的脚本中,它假定您正在尝试下载除Google文档以外的文件。

References: 参考文献:

If I misunderstood your question and this was not the result you want, I apologize. 如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出驱动器 D 中的所有文件和文件夹目录并使用 php 下载 - how to list all file and folder directory from drive D and download it using php 如何使用PHP下载Google驱动器文件内容并保存为相应的文件格式 - How to download the google drive file content and save into corresponding file format using PHP Google Drive PHP API下载文件 - Google Drive PHP API Download file 如何使用 php 删除 google drive 共享驱动器文件夹中的文件? - How to delete a file in a google drive shared drive folder using php? 使用 javascript 下载 Google Drive 文件 - Download Google Drive file using javascript 如何使用php获取Google驱动器中文件的文件ID - how to get the file id of a file in google drive using php 如何使用PHP在Google Drive SDK中创建PDF文件 - How to create file pdf file in google drive sdk using php 如何在使用 Google Drive API 将文件上传到 Google Drive 时设置自定义文件 ID? (PHP) - How To Set Custom File ID While Uploading a File To Google Drive Using Google Drive API? (PHP) 我们如何使用谷歌文档 api 和 PHP 将谷歌文档下载到我们的本地(计算机)/硬盘? - How we can download a google docs into our local (computer)/hard drive using google docs api with PHP? 如何使用PHP获取在Google Drive SDK中上传的文件URL - How to get the file URL uploaded in google drive SDK using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM