0
votes

I have applied file insertion code i.e to upload csv files to the google drive. All is working perfectly but I am getting duplicate files instead of getting one at a time. I have to upload two different files at a time but I am getting 4 files, need help in this case

This is google.php file where I have included "Google_Client.php","Google_DriveService.php" and file "insert.php" for the insertion in google drive which contain function 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
Can you give an example of your code?pilsetnieks

1 Answers

0
votes

Once OAuth 2.0 callback is handled, you need to stop executing. header(...) doesn't stop the script and inserts 2 files. Once the first execution is finished, redirection inserts the same files again.