簡體   English   中英

將多個 Google Drive 文件下載為壓縮文件

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

用戶登錄門戶后,將顯示 PDF 報告列表。

為了下載需要的報告,用戶可以選中/取消選中與每個報告相關聯的框。

例如,

列表中有 10 個報告。 用戶選擇了 7 個報告。 點擊下載。 此工作流程應下載包含所有選定報告 (7) 的壓縮文件,而不是單獨下載每個文件。

上例中的這 10 個報告存儲在 Google Drive 中。 我們將 Google 下載 URL 存儲在數據庫中。 使用這個下載地址,我們需要完成上述結果。

嘗試使用 Google Drive API 快速入門參考。 錯誤:403 在第二次嘗試將文件保存在文件系統中時命中。

在第三輪運行腳本時,PHP cURL 實現失敗,狀態碼為 403。

基本上,計划是將每個選定的文件保存在文件系統的文件夾中。 然后,壓縮文件夾並下載 zip。

這是我最近嘗試過的,

<?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);
}

?>

請給出完成結果的方向。

謝謝

正如@DalmTo 所說,API 不會讓您以 zip格式批量下載多個文件,您可以做的是在 Drive 內創建一個文件夾並將該文件夾下載為 zip 文件。

@Tanaike 在這個答案中提供了更多信息:

將文件夾下載為 Zip Google Drive API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM