0

I would like to force download files from Google Drive on a PC client via this script:

<?php 

//Get info about the file from a previous page (its name, type e.g. "rar", and file id which is the $download)
$download = $_GET['download'];
$name = $_GET['name'];
$type = $_GET['type'];

//Get json info about the file on drive, used to get its size in bytes
$json = file_get_contents('https://www.googleapis.com/drive/v2/files/'.$download.'?key=MYAPIKEY');
$obj = json_decode($json);
$size=$obj->fileSize;

//Download the file
$file = 'https://www.googleapis.com/drive/v3/files/'.$download.'?key=MYAPIKEY&alt=media';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$name.'.'.$type.'"');    
header('Content-Length: '.$size);
readfile($file);
exit;?>

The file gets corrupted when downloaded, any help? [EDIT] I got it to work, if someone else would like to know how to do this: add header ("Cache-Control: must-revalidate, post-check=0, pre-check=0"); at the beginning of the header lines.

4
  • I accidentely posted the question without actually having written the problem and now I don't know how to edit/delete it. If somemone is still interested: when I download files this way, they get somehow corrupted. Jars, exes or rars, all of them can't be opened.
    – user10392610
    Sep 20, 2018 at 17:03
  • Welcome to StackOverflow! Please See I've thought better of my question and search for But there are some other things you can do: Sep 20, 2018 at 18:14
  • It's just the link edit or delete below your question. Sep 20, 2018 at 18:16
  • @help-info.de thanks, these buttons are just too much visible
    – user10392610
    Sep 21, 2018 at 19:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.