简体   繁体   English

将多个 Google Drive 文件下载为压缩文件

[英]Download Multiple Google Drive Files as a Zipped File

Once the user login to the portal, a list of PDF reports are displayed.用户登录门户后,将显示 PDF 报告列表。

In order to download the reports in demand, user can check/uncheck the box associated to each report.为了下载需要的报告,用户可以选中/取消选中与每个报告相关联的框。

For Instance,例如,

There are 10 reports in the list.列表中有 10 个报告。 User has selected 7 reports.用户选择了 7 个报告。 Clicked Download.点击下载。 This workflow should result in the download of a zipped file which comprises of all the selected reports(7) rather than downloading each file individually.此工作流程应下载包含所有选定报告 (7) 的压缩文件,而不是单独下载每个文件。

These 10 reports in the above example are stored in the Google Drive.上例中的这 10 个报告存储在 Google Drive 中。 We store the Google download URL in database.我们将 Google 下载 URL 存储在数据库中。 Using this download URL we need to accomplish the aforesaid result.使用这个下载地址,我们需要完成上述结果。

Tried using Google Drive API Quickstart Reference.尝试使用 Google Drive API 快速入门参考。 Error: 403 hit at the second attempt to save the files in file system.错误:403 在第二次尝试将文件保存在文件系统中时命中。

PHP cURL implementation failed with 403 status code at the third round of running the script.在第三轮运行脚本时,PHP cURL 实现失败,状态码为 403。

Basically, the plan was to save each selected file inside a folder in the file system.基本上,计划是将每个选定的文件保存在文件系统的文件夹中。 Then, Zip the folder and download the zip.然后,压缩文件夹并下载 zip。

Here is what I have tried recently,这是我最近尝试过的,

<?php

define('SAVE_REPORT_DIR', getcwd(). '/pathtosave/'. time());

function fs_report_save($fileUrl) 
{
    static $counter = 1;

    if (!file_exists(SAVE_REPORT_DIR)) {
        mkdir(SAVE_REPORT_DIR, 0777, true);
    }

    //The path & filename to save to.
    $saveTo = SAVE_REPORT_DIR. '/'. time(). '.pdf';

    //Open file handler.
    $fp = fopen($saveTo, 'w+');

    //If $fp is FALSE, something went wrong.
    if($fp === false){
        throw new Exception('Could not open: ' . $saveTo);
    }

    //Create a cURL handle.
    $ch = curl_init($fileUrl);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //Pass our file handle to cURL.
    curl_setopt($ch, CURLOPT_FILE, $fp);

    //Timeout if the file doesn't download after 20 seconds.
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    //Execute the request.
    curl_exec($ch);

    //If there was an error, throw an Exception
    if(curl_errno($ch)){
        throw new Exception(curl_error($ch));
    }

    //Get the HTTP status code.
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    //Close the cURL handler.
    curl_close($ch);

    //Close the file handler.
    fclose($fp);

    if($statusCode == 200){
        echo 'File: '. $saveTo .'. Downloaded!<br>';
    } else{
        echo "Status Code: " . $statusCode;
    }
}

$reports = array(
    'https://drive.google.com/uc?id=a&export=download',
    'https://drive.google.com/uc?id=b&export=download',
    'https://drive.google.com/uc?id=c&export=download'
);

foreach($reports as $report) {
    fs_report_save($report);
}

?>

Please give a direction to accomplish the result.请给出完成结果的方向。

Thanks谢谢

As @DalmTo has said, the API is not going to let you download multiples files in bulk as a zip, what you can do is create a Folder inside Drive and download that folder as zip.正如@DalmTo 所说,API 不会让您以 zip格式批量下载多个文件,您可以做的是在 Drive 内创建一个文件夹并将该文件夹下载为 zip 文件。

There is a ton more information in this answer by @Tanaike: @Tanaike 在这个答案中提供了更多信息:

Download Folder as Zip Google Drive API 将文件夹下载为 Zip Google Drive API

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM