Upload dokumen ke folder terntentu di google drive. Bagaimana caranya ?, terlebih dahulu login ke akun google drive masing-masing –> drive.google.com
1. Setelah itu, buat sebuah folder di google drive. Klik tanda centang –> Pilih More –> Share –> Share..Pada bagian Who has Access, Pilih Sharing Settings yang diinginkan, misalnya memilih Public on The Web. Copy link share dan simpan di notepad
2. Download google api php client library di –> https://code.google.com/p/google-api-php-client/ . Pilih latest release
3. Buat susunan file seperti ini, kumpulkan dalam satu folder proyek
4. isi file index.php dengan code berikut
<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('Client Id Anda');
$client->setClientSecret('Client Secret Anda');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
if (!empty($_POST)) {
$fileName = $_FILES['dokumen']['name'];
if (move_uploaded_file($_FILES['dokumen']['tmp_name'], $fileName)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$parentId="Id Folder Anda di Google Drive";
$file_path = str_replace('/','\\',dirname($_SERVER['SCRIPT_FILENAME'])).'/'.$fileName;
$mime_type = finfo_file($finfo, $file_path);
//function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_DriveFile();
$file->setTitle($fileName);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($fileName);
$createdFile = $service->files->insert($file, array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type,
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
//}
finfo_close($finfo);
unlink($fileName);
header('location:'.$url);exit;
}
include 'index.phtml';
?>
5. isi file index.phtml dengan form upload
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Google Drive Example App</title>
</head>
<body>
<form method="post" action="<?php echo $url; ?>">
<input type="submit" value="enviar" name="submit">
</form>
</body>
</html>
Selesai
Bila mengalami kendala dalam pembuatan, bisa tengok google drive API Reference –> https://developers.google.com/drive/v2/reference/

