simple-file-upload.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. unset($_SESSION['upload_token']);
  61. }
  62. } else {
  63. $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
  64. $authUrl = $client->createAuthUrl();
  65. }
  66. /************************************************
  67. * If we're signed in then lets try to upload our
  68. * file. For larger files, see fileupload.php.
  69. ************************************************/
  70. if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
  71. // We'll setup an empty 1MB file to upload.
  72. DEFINE("TESTFILE", 'testfile-small.txt');
  73. if (!file_exists(TESTFILE)) {
  74. $fh = fopen(TESTFILE, 'w');
  75. fseek($fh, 1024 * 1024);
  76. fwrite($fh, "!", 1);
  77. fclose($fh);
  78. }
  79. // This is uploading a file directly, with no metadata associated.
  80. $file = new Google\Service\Drive\DriveFile();
  81. $result = $service->files->create(
  82. $file,
  83. [
  84. 'data' => file_get_contents(TESTFILE),
  85. 'mimeType' => 'application/octet-stream',
  86. 'uploadType' => 'media'
  87. ]
  88. );
  89. // Now lets try and send the metadata as well using multipart!
  90. $file = new Google\Service\Drive\DriveFile();
  91. $file->setName("Hello World!");
  92. $result2 = $service->files->create(
  93. $file,
  94. [
  95. 'data' => file_get_contents(TESTFILE),
  96. 'mimeType' => 'application/octet-stream',
  97. 'uploadType' => 'multipart'
  98. ]
  99. );
  100. }
  101. ?>
  102. <div class="box">
  103. <?php if (isset($authUrl)) : ?>
  104. <div class="request">
  105. <a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
  106. </div>
  107. <?php elseif ($_SERVER['REQUEST_METHOD'] == 'POST') : ?>
  108. <div class="shortened">
  109. <p>Your call was successful! Check your drive for the following files:</p>
  110. <ul>
  111. <li><a href="https://drive.google.com/open?id=<?= $result->id ?>" target="_blank"><?= $result->name ?></a></li>
  112. <li><a href="https://drive.google.com/open?id=<?= $result2->id ?>" target="_blank"><?= $result2->name ?></a></li>
  113. </ul>
  114. </div>
  115. <?php else : ?>
  116. <form method="POST">
  117. <input type="submit" value="Click here to upload two small (1MB) test files" />
  118. </form>
  119. <?php endif ?>
  120. </div>