fileupload.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
  20. /************************************************
  21. We'll setup an empty 20MB file to upload.
  22. ************************************************/
  23. DEFINE("TESTFILE", 'testfile.txt');
  24. if (!file_exists(TESTFILE)) {
  25. $fh = fopen(TESTFILE, 'w');
  26. fseek($fh, 1024*1024*20);
  27. fwrite($fh, "!", 1);
  28. fclose($fh);
  29. }
  30. /************************************************
  31. ATTENTION: Fill in these values! Make sure
  32. the redirect URI is to this page, e.g:
  33. http://localhost:8080/fileupload.php
  34. ************************************************/
  35. $client_id = '<YOUR_CLIENT_ID>';
  36. $client_secret = '<YOUR_CLIENT_SECRET>';
  37. $redirect_uri = '<YOUR_REDIRECT_URI>';
  38. $client = new Google_Client();
  39. $client->setClientId($client_id);
  40. $client->setClientSecret($client_secret);
  41. $client->setRedirectUri($redirect_uri);
  42. $client->addScope("https://www.googleapis.com/auth/drive");
  43. $service = new Google_Service_Drive($client);
  44. if (isset($_REQUEST['logout'])) {
  45. unset($_SESSION['upload_token']);
  46. }
  47. if (isset($_GET['code'])) {
  48. $client->authenticate($_GET['code']);
  49. $_SESSION['upload_token'] = $client->getAccessToken();
  50. $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  51. header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  52. }
  53. if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
  54. $client->setAccessToken($_SESSION['upload_token']);
  55. if ($client->isAccessTokenExpired()) {
  56. unset($_SESSION['upload_token']);
  57. }
  58. } else {
  59. $authUrl = $client->createAuthUrl();
  60. }
  61. /************************************************
  62. If we're signed in then lets try to upload our
  63. file.
  64. ************************************************/
  65. if ($client->getAccessToken()) {
  66. $file = new Google_Service_Drive_DriveFile();
  67. $file->title = "Big File";
  68. $chunkSizeBytes = 1 * 1024 * 1024;
  69. // Call the API with the media upload, defer so it doesn't immediately return.
  70. $client->setDefer(true);
  71. $request = $service->files->insert($file);
  72. // Create a media file upload to represent our upload process.
  73. $media = new Google_Http_MediaFileUpload(
  74. $client,
  75. $request,
  76. 'text/plain',
  77. null,
  78. true,
  79. $chunkSizeBytes
  80. );
  81. $media->setFileSize(filesize(TESTFILE));
  82. // Upload the various chunks. $status will be false until the process is
  83. // complete.
  84. $status = false;
  85. $handle = fopen(TESTFILE, "rb");
  86. while (!$status && !feof($handle)) {
  87. // read until you get $chunkSizeBytes from TESTFILE
  88. // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
  89. // An example of a read buffered file is when reading from a URL
  90. $chunk = readVideoChunk($handle, $chunkSizeBytes);
  91. $status = $media->nextChunk($chunk);
  92. }
  93. // The final value of $status will be the data from the API for the object
  94. // that has been uploaded.
  95. $result = false;
  96. if ($status != false) {
  97. $result = $status;
  98. }
  99. fclose($handle);
  100. }
  101. echo pageHeader("File Upload - Uploading a large file");
  102. if (strpos($client_id, "googleusercontent") == false) {
  103. echo missingClientSecretsWarning();
  104. exit;
  105. }
  106. function readVideoChunk ($handle, $chunkSize)
  107. {
  108. $byteCount = 0;
  109. $giantChunk = "";
  110. while (!feof($handle)) {
  111. // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
  112. $chunk = fread($handle, 8192);
  113. $byteCount += strlen($chunk);
  114. $giantChunk .= $chunk;
  115. if ($byteCount >= $chunkSize)
  116. {
  117. return $giantChunk;
  118. }
  119. }
  120. return $giantChunk;
  121. }
  122. ?>
  123. <div class="box">
  124. <div class="request">
  125. <?php
  126. if (isset($authUrl)) {
  127. echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  128. }
  129. ?>
  130. </div>
  131. <div class="shortened">
  132. <?php
  133. if (isset($result) && $result) {
  134. var_dump($result);
  135. }
  136. ?>
  137. </div>
  138. </div>
  139. <?php
  140. echo pageFooter(__FILE__);