simplefileupload.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 1MB file to upload.
  22. ************************************************/
  23. DEFINE("TESTFILE", 'testfile-small.txt');
  24. if (!file_exists(TESTFILE)) {
  25. $fh = fopen(TESTFILE, 'w');
  26. fseek($fh, 1024 * 1024);
  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. For larger files, see fileupload.php.
  64. ************************************************/
  65. if ($client->getAccessToken()) {
  66. // This is uploading a file directly, with no metadata associated.
  67. $file = new Google_Service_Drive_DriveFile();
  68. $result = $service->files->insert(
  69. $file,
  70. array(
  71. 'data' => file_get_contents(TESTFILE),
  72. 'mimeType' => 'application/octet-stream',
  73. 'uploadType' => 'media'
  74. )
  75. );
  76. // Now lets try and send the metadata as well using multipart!
  77. $file = new Google_Service_Drive_DriveFile();
  78. $file->setTitle("Hello World!");
  79. $result2 = $service->files->insert(
  80. $file,
  81. array(
  82. 'data' => file_get_contents(TESTFILE),
  83. 'mimeType' => 'application/octet-stream',
  84. 'uploadType' => 'multipart'
  85. )
  86. );
  87. }
  88. echo pageHeader("File Upload - Uploading a small file");
  89. if (strpos($client_id, "googleusercontent") == false) {
  90. echo missingClientSecretsWarning();
  91. exit;
  92. }
  93. ?>
  94. <div class="box">
  95. <div class="request">
  96. <?php
  97. if (isset($authUrl)) {
  98. echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  99. }
  100. ?>
  101. </div>
  102. <div class="shortened">
  103. <?php
  104. if (isset($result) && $result) {
  105. var_dump($result->title);
  106. var_dump($result2->title);
  107. }
  108. ?>
  109. </div>
  110. </div>
  111. <?php
  112. echo pageFooter(__FILE__);