simple-file-upload.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 __DIR__ . '/vendor/autoload.php';
  18. include_once "base.php";
  19. echo pageHeader("File Upload - Uploading a simple file");
  20. /*************************************************
  21. * Ensure you've downloaded your oauth credentials
  22. ************************************************/
  23. if (!$oauth_credentials = getOAuthCredentialsFile()) {
  24. echo missingOAuth2CredentialsWarning();
  25. return;
  26. }
  27. /************************************************
  28. * The redirect URI is to the current page, e.g:
  29. * http://localhost:8080/simple-file-upload.php
  30. ************************************************/
  31. $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  32. $client = new Google\Client();
  33. $client->setAuthConfig($oauth_credentials);
  34. $client->setRedirectUri($redirect_uri);
  35. $client->addScope("https://www.googleapis.com/auth/drive");
  36. $service = new Google\Service\Drive($client);
  37. // add "?logout" to the URL to remove a token from the session
  38. if (isset($_REQUEST['logout'])) {
  39. unset($_SESSION['upload_token']);
  40. }
  41. /************************************************
  42. * If we have a code back from the OAuth 2.0 flow,
  43. * we need to exchange that with the
  44. * Google\Client::fetchAccessTokenWithAuthCode()
  45. * function. We store the resultant access token
  46. * bundle in the session, and redirect to ourself.
  47. ************************************************/
  48. if (isset($_GET['code'])) {
  49. $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
  50. $client->setAccessToken($token);
  51. // store in the session also
  52. $_SESSION['upload_token'] = $token;
  53. // redirect back to the example
  54. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  55. }
  56. // set the access token as part of the client
  57. if (!empty($_SESSION['upload_token'])) {
  58. $client->setAccessToken($_SESSION['upload_token']);
  59. if ($client->isAccessTokenExpired()) {
  60. if ($client->getRefreshToken()) {
  61. $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
  62. $_SESSION['upload_token'] = $client->getAccessToken();
  63. } else {
  64. unset($_SESSION['upload_token']);
  65. }
  66. }
  67. } else {
  68. $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
  69. $authUrl = $client->createAuthUrl();
  70. }
  71. /************************************************
  72. * If we're signed in then lets try to upload our
  73. * file. For larger files, see fileupload.php.
  74. ************************************************/
  75. if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
  76. // We'll setup an empty 1MB file to upload.
  77. DEFINE("TESTFILE", 'testfile-small.txt');
  78. if (!file_exists(TESTFILE)) {
  79. $fh = fopen(TESTFILE, 'w');
  80. fseek($fh, 1024 * 1024);
  81. fwrite($fh, "!", 1);
  82. fclose($fh);
  83. }
  84. // This is uploading a file directly, with no metadata associated.
  85. $file = new Google\Service\Drive\DriveFile();
  86. $result = $service->files->create(
  87. $file,
  88. [
  89. 'data' => file_get_contents(TESTFILE),
  90. 'mimeType' => 'application/octet-stream',
  91. 'uploadType' => 'media'
  92. ]
  93. );
  94. // Now lets try and send the metadata as well using multipart!
  95. $file = new Google\Service\Drive\DriveFile();
  96. $file->setName("Hello World!");
  97. $result2 = $service->files->create(
  98. $file,
  99. [
  100. 'data' => file_get_contents(TESTFILE),
  101. 'mimeType' => 'application/octet-stream',
  102. 'uploadType' => 'multipart'
  103. ]
  104. );
  105. }
  106. ?>
  107. <div class="box">
  108. <?php if (isset($authUrl)) : ?>
  109. <div class="request">
  110. <a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
  111. </div>
  112. <?php elseif ($_SERVER['REQUEST_METHOD'] == 'POST') : ?>
  113. <div class="shortened">
  114. <p>Your call was successful! Check your drive for the following files:</p>
  115. <ul>
  116. <li><a href="https://drive.google.com/open?id=<?= $result->id ?>" target="_blank"><?= $result->name ?></a></li>
  117. <li><a href="https://drive.google.com/open?id=<?= $result2->id ?>" target="_blank"><?= $result2->name ?></a></li>
  118. </ul>
  119. </div>
  120. <?php else : ?>
  121. <form method="POST">
  122. <input type="submit" value="Click here to upload two small (1MB) test files" />
  123. </form>
  124. <?php endif ?>
  125. </div>