douwen3083 2018-09-20 16:57
浏览 29

使用PHP和REST API密钥从Google云端硬盘下载

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
    • ¥15 NX MCD仿真与博途通讯不了啥情况
    • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
    • ¥15 gradio的web端页面格式不对的问题
    • ¥15 求大家看看Nonce如何配置
    • ¥15 Matlab怎么求解含参的二重积分?
    • ¥15 苹果手机突然连不上wifi了?
    • ¥15 cgictest.cgi文件无法访问
    • ¥20 删除和修改功能无法调用
    • ¥15 kafka topic 所有分副本数修改