如何使用Google Drive API在我的云端硬盘(PHP)中上传文件?

问题描述:

我想制作一个网页,其中将有一个文件输入框,并且用户上传的文件应保存到我的Google云端硬盘中.

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.

如何使用PHP实现它?

How do I achieve it using PHP?

我不想使用作曲家

我需要推荐一篇带有一些适应症的好文章

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

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! Please tell me how to make a simple uploading PHP page.

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

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

Upload to Google Drive with the Google Drive API - without composer

What you need for integrating the feature with a Website:

  • 安装 google-api-php-client
  • 安装 Google_DriveService客户端
  • 无论您如何将文件上传到Google云端硬盘,都需要某种凭据来表明您可以访问所涉及的云端硬盘(也就是说,您需要以自己的身份进行身份验证).为此:
    • 如果尚未完成-设置(免费)Google Cloud控制台.
    • 创建一个项目.
    • 启用 Drive API .
    • 设置同意屏幕.
    • 转到 APIs&服务->凭据 +创建凭据
    • 有多种可能,根据您的情况,创建一个 OAuth客户端ID 并选择应用程序类型:Web应用程序
    • 使用以下格式指定您网站的网址:授权的JavaScript来源授权的重定向URI
    • 创建客户端后-记下客户端ID 客户端密码
    • Install the google-api-php-client
    • Install the Google_DriveService client
    • 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:
      • If not already done - set up (for free) the Google Cloud console.
      • Create a project.
      • Enable the Drive API.
      • Set up a consent screen.
      • Go to APIs & Services -> Credentials and +Create Credentials
      • There are several possibilities, in your case, it makes sense to create an OAuth client ID and chose Application type: Web Application
      • Specify the URL of your website with the form as Authorized JavaScript origins and Authorized redirect URIs
      • After creating the client - note down the client ID and client secret

      现在,您可以将Google的样本与OAuth2客户端进行身份验证进行汇总创建 Google云端硬盘服务对象上传到Google云端硬盘,然后将其合并到

      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.

      将这些代码段拼凑在一起,如下所示:

      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>
      

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