【问题标题】:Not able to uplaod file on googledrive?无法在谷歌驱动器上上传文件?
【发布时间】: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


【解决方案1】:

您正在尝试使用来自 google 的分段(参见 this)上传方法。

// Getting Files
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
  if ($file != '.' && $file != '..') {
            $files[] = $file;
  }
}
$dir->close();

if (!empty($_POST)) {

    // set access token
    $client->setAccessToken($_SESSION['accessToken']);

    // service object
    $service = new Google_DriveService($client);

    // drive file object
    $file = new Google_DriveFile();

    foreach ($files as $file_name) {

        $file_path = 'files/'.$file_name;

        $file->setTitle($file_name);

        // Use create instead of insert 
        // store response on $result array
        $result[] = $service->files->create(
             $file,
             array(
                'data' => file_get_contents($file_path),
                'mimeType' => 'application/octet-stream',
                'uploadType' => 'multipart'
             )
        );
    }

    // check response
    print_r($result);       
}

您必须为所有类型的文件设置'mimeType' =&gt; 'application/octet-stream'。如果您要发送元数据,请使用uploadtype as multipart!。

我没有测试过这段代码。让我知道它是否适合你

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    相关资源
    最近更新 更多