尝试将CS​​V文件上传到Google电子表格时获取重复文件

时间:2013-04-14 22:50:52

标签: php google-drive-api

我已应用文件插入代码,即将csv文件上传到google驱动器。一切都工作得很好,但我得到重复的文件,而不是一次得到一个。我必须一次上传两个不同的文件,但我得到4个文件,在这种情况下需要帮助

这是google.php文件,其中包含" Google_Client.php"," Google_DriveService.php"和文件" insert.php"在谷歌驱动器中插入包含函数insertFile($ service,$ title,$ description,$ parentId,$ mimeType,$ filename)

<?php 
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require 'insert.php';
session_start();

$title = 'Total Likes/Reblogs';
$description = 'Total Likes/Reblogs';

$title1 = 'Total Likes/Reblogs per post';
$description1 = 'Total Likes/Reblogs per post';

$parentId = ''; 
$mimeType = 'text/csv'; 
$filename = 'total.csv';
$fileNamePerPost = 'totalPerPost.csv';

$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
$service = new Google_DriveService($client);

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

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

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {

    insertFile($service, $title, $description, $parentId, $mimeType, $filename);
    insertFile($service, $title1, $description1, $parentId, $mimeType, $fileNamePerPost);

} else {
  $state = mt_rand();
  $client->setState($state);
  echo $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>

1 个答案:

答案 0 :(得分:0)

处理OAuth 2.0回调后,您需要停止执行。 header(...)不会停止脚本并插入2个文件。第一次执行完成后,重定向会再次插入相同的文件。