我是PHP新手,我一直在使用Google Drive PHP API。
这是我的代码:
function downloadFile1($service,$downloadUrl,$client) {
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$SignhttpRequest = $client->getAuth()->sign($request);
$httpRequest = $client->getIo()->makeRequest($SignhttpRequest);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
到目前为止一切顺利。但我想得到一个对话框将文件保存到我的电脑(当我得到返回结果时)。我怎么能得到它?
[如果我执行
$content = $httpRequest->getResponseBody();
print_r($content);]
我可以在网页上看到该文件的内容,但我想下载它!]
提前致谢!
答案 0 :(得分:0)
不是使用PHP将文件下载到服务器,而是在页面上向用户显示指向文件webContentLink
的锚点。单击该链接会将文件下载到用户的计算机。
答案 1 :(得分:-1)
大家好吧也许这段代码可以帮助人们使用谷歌硬盘作为一项服务,可以与cronjob / cli一起上传,删除和下载文件大小无限的文件。
它还没有完成,但它是谷歌apiclient的包装。 并可用于上传,下载和删除文件。
<?php
namespace App\Model\Google;
class Drive
{
public $pixie;
protected $_config;
protected $_client;
protected $_service;
public function __construct(\App\Pixie $pixie)
{
$this->pixie = $pixie;
$this->_config = $this->pixie->config->get('googledrive');
$this->_client = new \Google_Client();
$this->_client->setClientId($this->_config['clientId']);
$this->_client->setClientSecret($this->_config['clientSecret']);
$this->_client->setRedirectUri($this->_config['redirectUri']); //
$this->_client->addScope($this->_config['scope']); // scope: https://www.googleapis.com/auth/drive
$this->_client->setAccessType($this->_config['accessType']); // offline
$token = $this->_client->refreshToken($this->_config['refreshToken']);
$this->_service = new \Google_Service_Drive($this->_client);
}
/**
* UPLOAD FILE
*/
public function uploadFile($filename, $file)
{
$gdriveFile = new \Google_Service_Drive_DriveFile();
$gdriveFile->title = $filename;
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$this->_client->setDefer(true);
$request = $this->_service->files->insert($gdriveFile);
$mimeType = 'text/plain';
// Create a media file upload to represent our upload process.
$media = new \Google_Http_MediaFileUpload(
$this->_client,
$request,
$mimeType,
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($file));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($file, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status !== false) {
$result = $status;
}
return $result;
}
/**
* DOWNLOAD FILE
*/
public function downloadUrl($fileId)
{
$file = $this->getFile($fileId);
if ($url = $file->getWebContentLink()) {
return $url;
}
return false;
}
/**
* GET FILE
* $file = $service->files->get($fileId);
*/
public function getFile($fileId)
{
try {
return $this->_service->files->get($fileId);
} catch(\Exception $e) {
throw new \Exception('Google Drive File Does Not Exist');
}
}
/**
* DELETE FILE
* $service->files->delete($fileId);
*/
public function deleteFile($fileId)
{
new \Debug();
}
/**
* FILE TO TRASH
* $service->files->trash($fileId);
*/
public function trashFile($fileId)
{
new \Debug();
}
/**
* EMPTY TRASH
* $service->files->emptyTrash();
*/
public function emptyTrash()
{
new \Debug();
}
/**
* FILE UNTRASH
* $service->files->untrash($fileId);
*/
public function untrashFile($fileId)
{
new \Debug();
}
/**
* LIST OF FILES
* $list = $service->files->listFiles();
*/
public function listOfFiles()
{
new \Debug();
}
}