文件上传与空的内容使用谷歌驱动器PHP API [英] file uploaded with empty content using google drive php api

查看:108
本文介绍了文件上传与空的内容使用谷歌驱动器PHP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码。使用谷歌驱动器php api成功上传/创建/插入文件在谷歌驱动器,但上传文件后,它的内容是空的。

 在session_start(); 
set_include_path(/ src /。PATH_SEPARATOR。get_include_path());
require_once'Google / Client.php';
require_once'Google / Service / Drive.php';

$ client_id ='XXXXXX';
$ client_secret ='XXXXXX';
$ redirect_uri ='XXXXXX';

$ client = new Google_Client();
$ client-> setClientId($ client_id);
$ client-> setClientSecret($ client_secret);
$ client-> setRedirectUri($ redirect_uri);
$ client-> addScope(https://www.googleapis.com/auth/drive);

$ service = new Google_Service_Drive($ client);

if(isset($ _ REQUEST ['logout'])){
unset($ _ SESSION ['upload_token']);


if(isset($ _ GET ['code'])){
$ client-> authenticate($ _ GET ['code']);
$ _SESSION ['upload_token'] = $ client-> getAccessToken();
$ redirect ='http://'。 $ _SERVER ['HTTP_HOST']。 $ _SERVER [ PHP_SELF];
header('Location:'。filter_var($ redirect,FILTER_SANITIZE_URL));

$ b $ if(isset($ _ SESSION ['upload_token'])&& $ _SESSION ['upload_token']){
$ client-> setAccessToken($ _SESSION [ 'upload_token']);
if($ client-> isAccessTokenExpired()){
unset($ _ SESSION ['upload_token']);
}
} else {
$ authUrl = $ client-> createAuthUrl();
}

$ filename =file.csv;

echoBR> stage 1;

if($ client-> getAccessToken())
{
$ data = file_get_contents($ filename);如果(isset($ data)&& $ data)
{
$ file = new Google_Service_Drive_DriveFile();


回声BR>阶段2;
if(isset($ file))
{
$ file - >的setTitle( gsexpo);
$档案 - > setMimeType( 应用程序/ vnd.google-apps.spreadsheet); ($set $($ file - > parentId)&&
$家长 - > setId($ file - > parentId);
$档案 - > setParents(阵列($亲本));
}

echoBR> stage 3;
$ b $ result2 = $ service-> files-> insert(
$ file,
array(
'data'=> $ data,
'mimeType'=>application / vnd.google-apps.spreadsheet,
'convert'=> true
));

echoResult:;
var_dump($ result2-> title);
}
else
{
echo没有文件对象;
}
}
else
{
echoFile read failed;
}
}
if(isset($ authUrl))
{echo< a href ='。$ authUrl。'>连接我!< A>中; }

这里是我用于此目的的我的google api php客户端库的链接。 / p>

https://github.com / google / google-api-php-client

解决方案

只需使用以下代码:

  $ result2 = $ service-> files-> create($ file,array(
'data'=> $ data
'mimeType'=>'application / vnd.google-apps.spreadsheet',
'uploadType'=>'multipart'
));

让我知道是否有问题。


Below is the code. which uploading/creating/inserting a file on the google drive using google drive php api successfully, but after uploading the file it's contents are empty.

session_start();
set_include_path("/src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client_id = 'XXXXXX';
$client_secret = 'XXXXXX';
$redirect_uri = 'XXXXXX';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['upload_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['upload_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
  $client->setAccessToken($_SESSION['upload_token']);
  if ($client->isAccessTokenExpired()) {
    unset($_SESSION['upload_token']);
  }
} else {
  $authUrl = $client->createAuthUrl();
}

$filename = "file.csv";

echo "<BR><BR>stage 1";

if ($client->getAccessToken())
{       
    $data = file_get_contents($filename);

    if (isset($data) && $data)
    {
        $file = new Google_Service_Drive_DriveFile();
        echo "<BR><BR>stage 2";
        if (isset($file))
        {            
            $file -> setTitle("gsexpo");
            $file -> setMimeType("application/vnd.google-apps.spreadsheet");

            if (isset($file -> parentId) && $file -> parentId != null) {
                $parent = new Google_ParentReference();
                $parent -> setId($file -> parentId);
                $file -> setParents(array($parent));
            }

            echo "<BR><BR>stage 3";

            $result2 = $service->files->insert(
                $file,
                array(
                  'data' => $data,
                  'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
                  'convert' => true
                ));

            echo "Result: ";            
            var_dump($result2->title);
        }
        else
        {
            echo "No file object";
        }
    }
    else
    {
        echo "File read failed";    
    }
}
if (isset($authUrl)) 
{     echo "<a href='".$authUrl."'>Connect Me!</a>";    }

here is the link of my google api php client library which i am using for this purpose.

https://github.com/google/google-api-php-client

解决方案

Simply use the code as below:

$result2 = $service->files->create($file, array(
            'data' => $data,
            'mimeType' => 'application/vnd.google-apps.spreadsheet',
            'uploadType' => 'multipart'
        ));

Let me know if any issue.

这篇关于文件上传与空的内容使用谷歌驱动器PHP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆