【问题标题】:How to use Google Drive API to upload files in my Drive (PHP)?如何使用 Google Drive API 在我的 Drive (PHP) 中上传文件?
【发布时间】:2020-09-01 23:08:41
【问题描述】:

我想做一个网页,里面会有一个文件输入框,用户上传的文件应该保存到我的Google Drive中。

如何使用 PHP 实现它?

我不想使用作曲家

我需要参考一篇有一些迹象的好文章

我在 Google 上查看过,但我发现文章可以写在别人的云端硬盘上,但不是我自己的。 另外,我查看了 Drive API 文档,但我认为它对我来说太专业了!请告诉我如何制作一个简单的上传PHP页面。

【问题讨论】:

  • 你需要先自己做一些研究和尝试。如果您在代码过程中遇到特定的问题,请回来向我们展示您尝试过的内容、预期结果以及您得到的结果。目前,这个问题太宽泛了。
  • “但我发现文章要写在别人的驱动器上,但不是我自己的” - 我不确定你的意思,但与上传到的唯一区别你的和别人的谷歌驱动器基本上是你配置的凭据。
  • 您需要使用服务帐号。在 Google 的 oAuth API 文档中有相关文档
  • 您链接的是第三方库,官方描述了使用 Drive API 将文件上传到 Google Drive 的方式here
  • “因为我不知道“作曲家”是什么” - 这与图书馆本身无关。 Composer 是 PHP 最常用的依赖管理器。就像用于 javascript 的 npm 一样。如果您打算使用 PHP 进行编程,那么学习使用 Composer 是一个好主意。这将有很大帮助。否则,您仍然可以使用该库。只需下载它并手动包含所有需要的文件。但是对于许多库来说这可能很麻烦,因为它们是在考虑自动加载(通过前作曲家)的情况下构建的。

标签: php google-drive-api


【解决方案1】:

使用 Google Drive API 上传到 Google Drive - 无需作曲家

将该功能与网站集成所需的条件:

  • 安装google-api-php-client
  • 安装Google_DriveService client
  • 无论您如何将文件上传到 Google 云端硬盘 - 您都需要某种凭据来证明您可以访问相关的云端硬盘(即您需要以自己的身份进行身份验证)。为此:
    • 如果尚未完成 - 设置(免费)Google Cloud 控制台。
    • 创建project
    • 启用Drive API
    • 设置consent screen
    • 转到APIs & Services -> Credentials+Create Credentials
    • 有几种可能性,在您的情况下,创建OAuth client ID 并选择Application type: Web Application 是有意义的
    • Authorized JavaScript originsAuthorized redirect URIs 的形式指定您网站的URL
    • 创建客户端后 - 记下 client IDclient secret

现在,您可以将 Google 的 authentication with the OAuth2 client 示例与 Google Drive service objectUploading to Google Drive 的创建放在一起,然后将其合并到 PHP File Upload 中。

将这些代码 sn-ps 修补在一起可能如下所示:

form.html

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

上传.php

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
//create a Google OAuth client
$client = new Google_Client();
$client->setClientId('YOUR CLIENT ID');
$client->setClientSecret('YOUR CLIENT SECRET');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if(empty($_GET['code']))
{
    $client->authenticate();
}

if(!empty($_FILES["fileToUpload"]["name"]))
{
  $target_file=$_FILES["fileToUpload"]["name"];
  // Create the Drive service object
  $accessToken = $client->authenticate($_GET['code']);
  $client->setAccessToken($accessToken);
  $service = new Google_DriveService($client);
  // Create the file on your Google Drive
  $fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'My file'));
  $content = file_get_contents($target_file);
  $mimeType=mime_content_type($target_file);
  $file = $driveService->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => $mimeType,
    'fields' => 'id'));
  printf("File ID: %s\n", $file->id);
}
?>

【讨论】:

  • 我无法将其标记为已接受,因为未显示接受按钮
  • @RaviPrakash 如果答案对您有帮助,您应该真正接受答案并奖励赏金,否则一半的赏金会丢失。
  • 是的,应该可以下载。
  • Client.php 不是一个文件夹,而是一个文件 - 至于文件夹 - 你可以手动创建它或更改路径变量以适应你现有的文件夹。
  • 您有关于如何安装“google-api-php-client”的说明吗?上面的链接不可用。
猜你喜欢
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多