| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2011 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
include_once "templates/base.php"; |
| 18 |
session_start(); |
| 19 |
|
| 20 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 21 |
|
| 22 |
/************************************************ |
| 23 |
We'll setup an empty 20MB file to upload. |
| 24 |
************************************************/ |
| 25 |
DEFINE("TESTFILE", 'testfile.txt'); |
| 26 |
if (!file_exists(TESTFILE)) { |
| 27 |
$fh = fopen(TESTFILE, 'w'); |
| 28 |
fseek($fh, 1024*1024*20); |
| 29 |
fwrite($fh, "!", 1); |
| 30 |
fclose($fh); |
| 31 |
} |
| 32 |
|
| 33 |
/************************************************ |
| 34 |
ATTENTION: Fill in these values! Make sure |
| 35 |
the redirect URI is to this page, e.g: |
| 36 |
http://localhost:8080/fileupload.php |
| 37 |
************************************************/ |
| 38 |
$client_id = '<YOUR_CLIENT_ID>'; |
| 39 |
$client_secret = '<YOUR_CLIENT_SECRET>'; |
| 40 |
$redirect_uri = '<YOUR_REDIRECT_URI>'; |
| 41 |
|
| 42 |
$client = new Google_Client(); |
| 43 |
$client->setClientId($client_id); |
| 44 |
$client->setClientSecret($client_secret); |
| 45 |
$client->setRedirectUri($redirect_uri); |
| 46 |
$client->addScope("https://www.googleapis.com/auth/drive"); |
| 47 |
$service = new Google_Service_Drive($client); |
| 48 |
|
| 49 |
if (isset($_REQUEST['logout'])) { |
| 50 |
unset($_SESSION['upload_token']); |
| 51 |
} |
| 52 |
|
| 53 |
if (isset($_GET['code'])) { |
| 54 |
$client->authenticate($_GET['code']); |
| 55 |
$_SESSION['upload_token'] = $client->getAccessToken(); |
| 56 |
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 57 |
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 58 |
} |
| 59 |
|
| 60 |
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { |
| 61 |
$client->setAccessToken($_SESSION['upload_token']); |
| 62 |
if ($client->isAccessTokenExpired()) { |
| 63 |
unset($_SESSION['upload_token']); |
| 64 |
} |
| 65 |
} else { |
| 66 |
$authUrl = $client->createAuthUrl(); |
| 67 |
} |
| 68 |
|
| 69 |
/************************************************ |
| 70 |
If we're signed in then lets try to upload our |
| 71 |
file. |
| 72 |
************************************************/ |
| 73 |
if ($client->getAccessToken()) { |
| 74 |
$file = new Google_Service_Drive_DriveFile(); |
| 75 |
$file->title = "Big File"; |
| 76 |
$chunkSizeBytes = 1 * 1024 * 1024; |
| 77 |
|
| 78 |
// Call the API with the media upload, defer so it doesn't immediately return. |
| 79 |
$client->setDefer(true); |
| 80 |
$request = $service->files->insert($file); |
| 81 |
|
| 82 |
// Create a media file upload to represent our upload process. |
| 83 |
$media = new Google_Http_MediaFileUpload( |
| 84 |
$client, |
| 85 |
$request, |
| 86 |
'text/plain', |
| 87 |
null, |
| 88 |
true, |
| 89 |
$chunkSizeBytes |
| 90 |
); |
| 91 |
$media->setFileSize(filesize(TESTFILE)); |
| 92 |
|
| 93 |
// Upload the various chunks. $status will be false until the process is |
| 94 |
// complete. |
| 95 |
$status = false; |
| 96 |
$handle = fopen(TESTFILE, "rb"); |
| 97 |
while (!$status && !feof($handle)) { |
| 98 |
// read until you get $chunkSizeBytes from TESTFILE |
| 99 |
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file |
| 100 |
// An example of a read buffered file is when reading from a URL |
| 101 |
$chunk = readVideoChunk($handle, $chunkSizeBytes); |
| 102 |
$status = $media->nextChunk($chunk); |
| 103 |
} |
| 104 |
|
| 105 |
// The final value of $status will be the data from the API for the object |
| 106 |
// that has been uploaded. |
| 107 |
$result = false; |
| 108 |
if ($status != false) { |
| 109 |
$result = $status; |
| 110 |
} |
| 111 |
|
| 112 |
fclose($handle); |
| 113 |
} |
| 114 |
echo pageHeader("File Upload - Uploading a large file"); |
| 115 |
if (strpos($client_id, "googleusercontent") == false) { |
| 116 |
echo missingClientSecretsWarning(); |
| 117 |
exit; |
| 118 |
} |
| 119 |
function readVideoChunk ($handle, $chunkSize) |
| 120 |
{ |
| 121 |
$byteCount = 0; |
| 122 |
$giantChunk = ""; |
| 123 |
while (!feof($handle)) { |
| 124 |
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file |
| 125 |
$chunk = fread($handle, 8192); |
| 126 |
$byteCount += strlen($chunk); |
| 127 |
$giantChunk .= $chunk; |
| 128 |
if ($byteCount >= $chunkSize) |
| 129 |
{ |
| 130 |
return $giantChunk; |
| 131 |
} |
| 132 |
} |
| 133 |
return $giantChunk; |
| 134 |
} |
| 135 |
?> |
| 136 |
<div class="box"> |
| 137 |
<div class="request"> |
| 138 |
<?php |
| 139 |
if (isset($authUrl)) { |
| 140 |
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; |
| 141 |
} |
| 142 |
?> |
| 143 |
</div> |
| 144 |
|
| 145 |
<div class="shortened"> |
| 146 |
<?php |
| 147 |
if (isset($result) && $result) { |
| 148 |
var_dump($result); |
| 149 |
} |
| 150 |
?> |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
<?php |
| 154 |
echo pageFooter(__FILE__); |
| 155 |
|