【问题标题】:Uploading a file using the Google Drive API v3 and php via html form使用 Google Drive API v3 和 php 通过 html 表单上传文件
【发布时间】:2020-05-29 23:13:32
【问题描述】:

我正在开发一个程序,该程序允许用户使用 HTML 表单选择文件,然后该程序会将所述文件上传到 Google Drive 并与某个其他用户共享。我已经能够在 Google Drive 中创建一个文件然后分享它;但是一旦我尝试上传一个文件,一切都走下坡路了。

这很奇怪,因为我认为上传文件的代码是正确的,所以问题必须在于我如何将文件从 HTML 表单获取到 PHP 文件。我试图回显 $_FILES['myfile']['name'] 没有任何反应,这在一定程度上证实了我的怀疑。

我将在下面上传两个代码 sn-ps,但为了简洁起见,我将省略所有 Google Drive api 身份验证,因为它有效。

index.php

      <form action="../Next.php" method="POST" enctype="multipart/form-data">
    <section class="file">
      <span>Please select the file to be uploaded </span>
      <br>
      <label for="myfile">Select a file:</label>
      <input type="file"  name="myfile" required  >
     </section>
    <section class="submission">
      <input type="submit" value="Submit">
    </section>
  </form>

Next.php

    $myfile=$_FILES['myfile']['tmp_name'];
    $type=mime_content_type($myfile);
    $drive = new Google_Service_Drive($client);
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle('Test');
    $file->setDescription('Test');
    $file->setMimeType($type);
    $data = file_get_contents($myfile);
    $uploadedFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => $type,
     ));

请记住,我已经省略了身份验证码,因为我知道这是可行的。

我不明白为什么这不起作用,从我通过浏览互联网收集到的信息来看,代码是正确的。我认为这个问题可能与 $_FILES['myfile'] 数组有关,但从所有在线文档来看,它似乎也是正确的。

【问题讨论】:

    标签: php forms google-drive-api


    【解决方案1】:

    如果还没有解决那么... 正如您提到的 $_FILES 是空的,那么我建议您查看项目中的文件位置。 假设您的index.phpPROJECT-&gt;HOME-&gt;index.php 目录中,那么您的Next.php 应该在PROJECT-&gt;Next.php 目录中。

    您还必须提交name=submit

    Next.php 中测试是否设置了表单

    if(isset($_POST['submit'])){ print_r($_FILES['myfile']); }

    如果文件没有上传,请告诉我问题。

    【讨论】:

      【解决方案2】:

      初始设置

      由于您的 HTML 文件规范,我假设您的项目结构如下:

      project/
      ├── templates/
      │   └── index.html
      └── Next.php
      

      所以我将使用命令在项目文件夹中启动一个php简单http服务器:php -S localhost:8000

      方法

      为了使其工作,您必须稍微调整您的Next.php 代码。我建议使用最新的 Drive APi 版本 (v3)。 v3 中没有方法insert(),但它有create()

      // Get the API client and construct the service object.
      $client = getClient();
      $service = new Google_Service_Drive($client);
      $myfile=$_FILES['myfile']['tmp_name'];
      
      $fileMetadata = new Google_Service_Drive_DriveFile(array(
          'name' => "Test",
      ));
      
      try{
      
          $newFile = $service->files->create(
              $fileMetadata,
              array(
                  'data' => file_get_contents($myfile),
                  'mimeType' => mime_content_type($myfile),
                  'uploadType' => 'multipart'
              )
          );
      
      
      } catch(Exception $e){
      
          echo 'An error ocurred : ' . $e->getMessage();
      
      }
      

      参考

      Drive v3 REST API

      【讨论】:

        【解决方案3】:

        尝试添加多部分

        $uploadedFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => $type,
          'uploadType' => 'multipart'
        ));
        

        【讨论】:

        • 这似乎并没有解决问题,我认为问题与 $data 有关,因为 $_FILES 超级全局变量似乎出于某种原因为空;所以我认为问题是这样的。如果有人知道为什么这将有很大帮助。在 php.ini 上,file_uploads 设置为 on,并设置了一个临时目录。
        • print_r($_FILES['myfile']['tmp_name']) 提供了什么?
        猜你喜欢
        • 2021-02-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2020-09-27
        • 2020-03-07
        • 2020-07-22
        • 1970-01-01
        相关资源
        最近更新 更多