Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Upload a PDF file to Google Drive
<?php
require 'google-api/apiClient.php';
require 'google-api/contrib/apiOauth2Service.php';
require 'google-api/contrib/apiDriveService.php';
$pdfFile = 'test.pdf';
// API Console: https://code.google.com/apis/console/
// Create an API project ("web applications") and put the client id and client secret in config.ini.
// Set up the Drive SDK in the API console.
// Create a Chrome extension, set the "container" and "api_console_project_id" parameters, and install it.
$config = parse_ini_file('config.ini'); // client_id, client_secret
// initialise the client
$client = new apiClient();
$client->setUseObjects(true);
$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['client_uri']);
$client->setAccessToken(authenticate($client));
// initialise the Google Drive service
$service = new apiDriveService($client);
// create and upload a new Google Drive file, including the data
try {
$file = new DriveFile;
$file->setTitle(basename($pdfFile));
$file->setMimeType('application/pdf');
$result = $service->files->insert($file, array('data' => file_get_contents($pdfFile), 'mimeType' => 'application/pdf'));
}
catch (Exception $e) {
print $e->getMessage();
}
print_r($result);
function authenticate($client, $file = 'token.json'){
if (file_exists($file)) return file_get_contents($file);
$_GET['code'] = ''; // insert the verification code here
// print the authentication URL
if (!$_GET['code']) {
exit($client->createAuthUrl(array('https://www.googleapis.com/auth/drive.file')) . "\n");
}
file_put_contents($file, $client->authenticate());
exit('Authentication saved to token.json - now run this script again.');
}
@arun7379

This comment has been minimized.

Show comment Hide comment
@arun7379

arun7379 Nov 25, 2015

Hi, I really appreciate your efforts for providing this code snippet,but can you please also send the download link to the complete api?

Hi, I really appreciate your efforts for providing this code snippet,but can you please also send the download link to the complete api?

@anatolinicolae

This comment has been minimized.

Show comment Hide comment
@joshiket

This comment has been minimized.

Show comment Hide comment
@joshiket

joshiket Feb 6, 2016

I am getting Undefined index: uploadType in Google\Service\Resource.php on line 221 exception, when I call the $service->files->insert() method, I have passed all the parameters as shown in the above code.
Solved
earlier I was using $service->files->insert($file, array('data' => $data, 'mimeType' => 'image/jpeg'));
changed it to $service->files->insert($file, array('data' => $data, 'mimeType' => 'image/jpeg','uploadType' => 'media'));

joshiket commented Feb 6, 2016

I am getting Undefined index: uploadType in Google\Service\Resource.php on line 221 exception, when I call the $service->files->insert() method, I have passed all the parameters as shown in the above code.
Solved
earlier I was using $service->files->insert($file, array('data' => $data, 'mimeType' => 'image/jpeg'));
changed it to $service->files->insert($file, array('data' => $data, 'mimeType' => 'image/jpeg','uploadType' => 'media'));

@shivanraptor

This comment has been minimized.

Show comment Hide comment
@shivanraptor

shivanraptor Mar 7, 2016

@joshiket To solve the warning, you should specify uploadType along with mimeType.
p.s. this piece of code is for V2; please migrate to V3.

@joshiket To solve the warning, you should specify uploadType along with mimeType.
p.s. this piece of code is for V2; please migrate to V3.

@bikeshtricon

This comment has been minimized.

Show comment Hide comment
@bikeshtricon

bikeshtricon Jun 10, 2016

can you please update the topic

can you please update the topic

@abhiagrawal87

This comment has been minimized.

Show comment Hide comment
@abhiagrawal87

abhiagrawal87 Aug 2, 2016

Hi, I need to setup Google Drive API for one of my project and following the documentation leads me nowhere. The API which I am getting do not have files which are included in the code. Can anyone guide me with the code and API as I am new to APIs.

I need to create a system wherein public users will submit their assignments by uploading Word/PDF files which will be transferred to common Google Drive of teacher and link of each uploaded file will be generated.

Please suggest. Thanks.

Hi, I need to setup Google Drive API for one of my project and following the documentation leads me nowhere. The API which I am getting do not have files which are included in the code. Can anyone guide me with the code and API as I am new to APIs.

I need to create a system wherein public users will submit their assignments by uploading Word/PDF files which will be transferred to common Google Drive of teacher and link of each uploaded file will be generated.

Please suggest. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment