简体   繁体   中英

Download Multiple Google Drive Files as a Zipped File

Once the user login to the portal, a list of PDF reports are displayed.

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. User has selected 7 reports. 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.

These 10 reports in the above example are stored in the Google Drive. We store the Google download URL in database. Using this download URL we need to accomplish the aforesaid result.

Tried using Google Drive API Quickstart Reference. Error: 403 hit at the second attempt to save the files in file system.

PHP cURL implementation failed with 403 status code at the third round of running the script.

Basically, the plan was to save each selected file inside a folder in the file system. Then, Zip the folder and download the 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.

There is a ton more information in this answer by @Tanaike:

Download Folder as Zip Google Drive API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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