passoftech

Google drive php upload for large files

Jun 30th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. <?php
  2.  
  3. class gdrive{
  4.  
  5. //credentials (get those from google developer console https://console.developers.google.com/)
  6.  
  7. var $clientId = "104799672448p42m.apps.googleusercontent.com";
  8.  
  9. var $clientSecret = "7DWVXT";
  10. var $redirectUri = 'http://localhost/google_drive2/gdrive_upload.php';
  11.  
  12. //variables
  13. var $fileRequest;
  14. var $mimeType;
  15. var $filename;
  16. var $path;
  17. var $client;
  18.  
  19.  
  20. function __construct(){
  21. error_reporting(E_ALL); ini_set('display_errors', 1);
  22. require_once 'src/Google/autoload.php'; // get from here https://github.com/google/google-api-php-client.git
  23. require_once 'src/Google/Client.php'; // get from here https://github.com/google/google-api-php-client.git
  24. require_once 'src/Google/Service.php'; // get from here https://github.com/google/google-api-php-client.git
  25. require_once 'src/Google/Service/Drive.php'; // get from here https://github.com/google/google-api-php-client.git
  26.  
  27. $this->client = new Google_Client();
  28. }
  29.  
  30.  
  31. function initialize(){
  32. //echo "<br/>initializing class\n";
  33. $client = $this->client;
  34.  
  35. $client->setClientId($this->clientId);
  36. $client->setClientSecret($this->clientSecret);
  37. $client->setRedirectUri($this->redirectUri);
  38. $client->addScope('https://www.googleapis.com/auth/drive',
  39. 'https://www.googleapis.com/auth/drive.appfolder',
  40. 'https://www.googleapis.com/auth/drive.file');
  41.  
  42.  
  43. $refreshToken = file_get_contents(__DIR__ . "/token.txt");
  44.  
  45. $client->refreshToken($refreshToken);
  46. $tokens = $client->getAccessToken();
  47.  
  48. $client->setAccessToken($tokens);
  49. //$client->setDefer(true);
  50. $this->processFile();
  51.  
  52. }
  53.  
  54. function processFile(){
  55.  
  56. $fileRequest = $this->fileRequest;
  57. //echo "Process File $fileRequest\n";
  58. $path_parts = pathinfo($fileRequest);
  59. $this->path = $path_parts['dirname'];
  60. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  61. $this->mimeType = finfo_file($finfo, $fileRequest);
  62. finfo_close($finfo);
  63.  
  64. //echo "Mime type is " . $this->mimeType . "\n";
  65.  
  66. $this->upload();
  67.  
  68. }
  69.  
  70.  
  71. /**
  72. * Get the folder ID if it exists, if it doesnt exist, create it and return the ID
  73. *
  74. * @param Google_DriveService $service Drive API service instance.
  75. * @param String $folderName Name of the folder you want to search or create
  76. * @param String $folderDesc Description metadata for Drive about the folder (optional)
  77. * @return Google_Drivefile that was created or got. Returns NULL if an API error occured
  78. */
  79. function getFolderExistsCreate($service, $folderName, $folderDesc) {
  80.  
  81.  
  82.  
  83. $parameters['q'] = "mimeType='application/vnd.google-apps.folder' and trashed=false";
  84. $files = $service->files->listFiles($parameters);
  85. $found = false;
  86. // Go through each one to see if there is already a folder with the specified name
  87.  
  88. $found = false;
  89. // Go through each one to see if there is already a folder with the specified name
  90. foreach ($files['items'] as $item) {
  91. if ($item['title'] == $folderName) {
  92. $found = true;
  93. return $item['id'];
  94. break;
  95. }
  96. }
  97. // If not, create one
  98. if ($found == false) {
  99. $folder = new Google_Service_Drive_DriveFile();
  100. //Setup the folder to create
  101. $folder->setTitle($folderName);
  102. if(!empty($folderDesc))
  103. $folder->setDescription($folderDesc);
  104. $folder->setMimeType('application/vnd.google-apps.folder');
  105. //Create the Folder
  106. try {
  107. $createdFile = $service->files->insert($folder, array(
  108. 'mimeType' => 'application/vnd.google-apps.folder',
  109. ));
  110. // Return the created folder's id
  111. return $createdFile->id;
  112. } catch (Exception $e) {
  113. //print "An error occurred: " . $e->getMessage();
  114. print_r($e);
  115. }
  116. }
  117. }
  118.  
  119. function upload(){
  120. $client = $this->client;
  121. $service = new Google_Service_Drive($client);
  122.  
  123.  
  124.  
  125.  
  126. $folderName=rand();
  127. $folderDesc='Folder Desc';
  128. //$client->setDefer(false);
  129. // Setup the folder you want the file in, if it is wanted in a folder
  130. if(isset($folderName)) {
  131. if(!empty($folderName)) {
  132. $parent = new Google_Service_Drive_ParentReference();
  133. $parent->setId($this->getFolderExistsCreate($service, $folderName, $folderDesc));
  134. $file = new Google_Service_Drive_DriveFile();
  135. $file->title = "a.csv";
  136. $chunkSizeBytes = 1 * 1024 * 1024;
  137. $file->setParents(array($parent));
  138. }
  139. }
  140.  
  141. //$client->setDefer(true);
  142.  
  143.  
  144.  
  145.  
  146. $fileRequest = $this->fileRequest;
  147. $mimeType = $this->mimeType;
  148. $request = $service->files->insert($file,array('mimeType'=> 'binary/octet-stream'));
  149.  
  150. // Create a media file upload to represent our upload process.
  151. $media = new Google_Http_MediaFileUpload(
  152. $client,
  153. $request,
  154. $mimeType,
  155. null,
  156. true,
  157. $chunkSizeBytes
  158. );
  159. $media->setFileSize(filesize($fileRequest));
  160.  
  161.  
  162. //$request = $this->google_drive->files->insert($file, array('mimeType'=> 'binary/octet-stream', 'mediaUpload' => $media));
  163.  
  164.  
  165. // Upload the various chunks. $status will be false until the process is
  166. // complete.
  167. $status = false;
  168. $handle = fopen($fileRequest, "rb");
  169.  
  170. // start uploading
  171. //echo "Uploading: " . $this->filename . "\n";
  172.  
  173. $filesize = filesize($fileRequest);
  174.  
  175. // while not reached the end of file marker keep looping and uploading chunks
  176. while (!$status && !feof($handle)) {
  177. $chunk = fread($handle, $chunkSizeBytes);
  178. $status = $media->nextChunk($chunk);
  179. }
  180.  
  181. // The final value of $status will be the data from the API for the object
  182. // that has been uploaded.
  183. $result = false;
  184. if($status != false) {
  185. $result = $status;
  186. }
  187.  
  188. fclose($handle);
  189. // Reset to the client to execute requests immediately in the future.
  190. $client->setDefer(false);
  191. ?>https://drive.google.com/open?id=<?= $result->id ?><?php
  192. }
  193.  
  194. }
  195.  
  196. ?>
Add Comment
Please, Sign In to add comment