简体   繁体   English

如何使用 Google Drive API 上传我的 Drive (PHP) 中的文件?

[英]How to use Google Drive API to upload files in my Drive (PHP)?

I want to make a webpage in which there will be an file input box, and the files which user uploads, should be saved into my Google Drive.我想制作一个网页,其中会有一个文件输入框,用户上传的文件应该保存到我的谷歌驱动器中。

How do I achieve it using PHP?我如何使用 PHP 实现它?

I don't want to use composer我不想使用作曲家

I need refreance to a good article with some indications我需要参考一篇有一些迹象的好文章

I checked on Google but I found articles to write on other's Drive, but not my own.我在谷歌上查了一下,但我发现文章要写在别人的云端硬盘上,而不是我自己的。 Also, I checked the Drive API documentation, but I think its too professional for me!另外,我查看了 Drive API 文档,但我认为它对我来说太专业了! Please tell me how to make a simple uploading PHP page.请告诉我如何制作一个简单的上传PHP页面。

Upload to Google Drive with the Google Drive API - without composer使用 Google Drive API 上传到 Google Drive - 无需作曲家

What you need for integrating the feature with a Website:将该功能与网站集成所需的条件:

  • Install the google-api-php-client安装google-api-php-client
  • Install the Google_DriveService client安装Google_DriveService 客户端
  • No matter how you upload files to Google Drive - you need some kind of credentials to show that you have access to the Drive in question (that is you need to authenticate as yourself).For this:无论您如何将文件上传到 Google 云端硬盘 - 您都需要某种凭据来表明您有权访问相关云端硬盘(即您需要以自己的身份进行身份验证)。为此:
    • If not already done - set up (for free) the Google Cloud console.如果尚未完成 - 设置(免费)Google Cloud 控制台。
    • Create a project .创建一个项目
    • Enable the Drive API .启用驱动器 API
    • Set up a consent screen .设置同意屏幕
    • Go to APIs & Services -> Credentials and +Create Credentials转到APIs & Services -> Credentials+Create Credentials
    • There are several possibilities, in your case, it makes sense to create an OAuth client ID and chose Application type: Web Application有几种可能性,在您的情况下,创建OAuth client ID并选择Application type: Web Application是有意义的Application type: Web Application
    • Specify the URL of your website with the form as Authorized JavaScript origins and Authorized redirect URIs将您网站的 URL 指定为Authorized JavaScript originsAuthorized redirect URIs
    • After creating the client - note down the client ID and client secret创建客户端后 - 记下client IDclient secret

Now, you can put together Google's sample for authentication with the OAuth2 client with the creation of the Google Drive service object and Uploading to Google Drive , then incorporate it into a PHP File Upload .现在,您可以通过创建Google Drive 服务对象Uploading to Google DriveGoogle 的 OAuth2 客户端身份验证示例放在一起,然后将其合并到PHP File Upload 中

Patching those code snippets together could look like following:将这些代码片段修补在一起可能如下所示:

form.html表单.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>

upload.php上传.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);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不询问的情况下将文件上传到Google云端硬盘文件夹(API PHP) - How to upload files to Google Drive folder without asking (API PHP) 如何使用 php Google Drive API 在 Google Drive 上上传文件 - How to upload a file on google drive with php Google Drive API 您如何使用 Google Drive API 永久授权用户帐户将文件上传到您的云端硬盘? - How do you use Google Drive API to permanently authorize an user account to upload files to your Drive? Google Drive API不允许未经PHP身份验证上传文件 - Google drive API is not allowing to upload files without authentication with PHP 将大文件上传到Google驱动器(PHP Google Drive API) - Upload large file to google drive (php google drive api) 使用 Google Drive 上传文件夹中的文件 API - Upload Files In Folders Using Google Drive API 通过API上传的Google云端硬盘文件未显示 - Google Drive Files Upload via API not showing 使用php curl将文件上传到Google云端硬盘 - Upload files to Google Drive using php curl 使用PHP和Drive API将文件上传到Google云端硬盘上的特定文件夹 - Upload file into particular folder on Google Drive using PHP and Drive API 如何使用带有访问令牌的php将文件上传到Google驱动器 - how to upload files to google drive using php with access token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM