0

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

Sagar
  • 1
  • 1

2 Answers2

0

You need to add the 'uploadType' parameter to your insert request array. eg:

$result2 = $service->files->insert(
    $file,
    array(
         'data' => $data,
         'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
         'convert' => true,
         'uploadType' => 'media',
    )
);
Synchro
  • 35,538
  • 15
  • 81
  • 104
0

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.

Jenil Kanani
  • 369
  • 1
  • 14