【发布时间】:2018-06-04 06:50:31
【问题描述】:
我想为此使用 PHP 将上传的文件保存在 Google 云端硬盘中。我没有收到任何回复。如果有人可以建议将不胜感激。下面是我的代码 我正在从这个 github 下载 API 我无法获得输出请帮助我
<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('<client Id>');
$client->setClientSecret('<client secret>');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
echo $_SESSION['accessToken'] ;
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
$dir->close();
if (!empty($_POST)) {
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
}
finfo_close($finfo);
header('location:'.$url);exit;
}
include 'index.phtml';
【问题讨论】:
-
在旁注中,但我认为您在添加标签时滑倒了,不小心将“javascript”作为其中之一提交。
-
@entiendoNull 感谢您的回复我不明白请逐步解释。我是否使用了正确版本的 google drive APK
-
这里有一些示例可能对您有所帮助github.com/google/google-api-php-client/tree/master/examples
-
运行此代码后谷歌驱动器重定向到正确的页面但没有得到响应并且图像没有上传到谷歌驱动器
标签: php google-api google-drive-api google-api-php-client