Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class gdrive{
- //credentials (get those from google developer console https://console.developers.google.com/)
- var $clientId = "104799672448p42m.apps.googleusercontent.com";
- var $clientSecret = "7DWVXT";
- var $redirectUri = 'http://localhost/google_drive2/gdrive_upload.php';
- //variables
- var $fileRequest;
- var $mimeType;
- var $filename;
- var $path;
- var $client;
- function __construct(){
- error_reporting(E_ALL); ini_set('display_errors', 1);
- require_once 'src/Google/autoload.php'; // get from here https://github.com/google/google-api-php-client.git
- require_once 'src/Google/Client.php'; // get from here https://github.com/google/google-api-php-client.git
- require_once 'src/Google/Service.php'; // get from here https://github.com/google/google-api-php-client.git
- require_once 'src/Google/Service/Drive.php'; // get from here https://github.com/google/google-api-php-client.git
- $this->client = new Google_Client();
- }
- function initialize(){
- //echo "<br/>initializing class\n";
- $client = $this->client;
- $client->setClientId($this->clientId);
- $client->setClientSecret($this->clientSecret);
- $client->setRedirectUri($this->redirectUri);
- $client->addScope('https://www.googleapis.com/auth/drive',
- 'https://www.googleapis.com/auth/drive.appfolder',
- 'https://www.googleapis.com/auth/drive.file');
- $refreshToken = file_get_contents(__DIR__ . "/token.txt");
- $client->refreshToken($refreshToken);
- $tokens = $client->getAccessToken();
- $client->setAccessToken($tokens);
- //$client->setDefer(true);
- $this->processFile();
- }
- function processFile(){
- $fileRequest = $this->fileRequest;
- //echo "Process File $fileRequest\n";
- $path_parts = pathinfo($fileRequest);
- $this->path = $path_parts['dirname'];
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $this->mimeType = finfo_file($finfo, $fileRequest);
- finfo_close($finfo);
- //echo "Mime type is " . $this->mimeType . "\n";
- $this->upload();
- }
- /**
- * Get the folder ID if it exists, if it doesnt exist, create it and return the ID
- *
- * @param Google_DriveService $service Drive API service instance.
- * @param String $folderName Name of the folder you want to search or create
- * @param String $folderDesc Description metadata for Drive about the folder (optional)
- * @return Google_Drivefile that was created or got. Returns NULL if an API error occured
- */
- function getFolderExistsCreate($service, $folderName, $folderDesc) {
- $parameters['q'] = "mimeType='application/vnd.google-apps.folder' and trashed=false";
- $files = $service->files->listFiles($parameters);
- $found = false;
- // Go through each one to see if there is already a folder with the specified name
- $found = false;
- // Go through each one to see if there is already a folder with the specified name
- foreach ($files['items'] as $item) {
- if ($item['title'] == $folderName) {
- $found = true;
- return $item['id'];
- break;
- }
- }
- // If not, create one
- if ($found == false) {
- $folder = new Google_Service_Drive_DriveFile();
- //Setup the folder to create
- $folder->setTitle($folderName);
- if(!empty($folderDesc))
- $folder->setDescription($folderDesc);
- $folder->setMimeType('application/vnd.google-apps.folder');
- //Create the Folder
- try {
- $createdFile = $service->files->insert($folder, array(
- 'mimeType' => 'application/vnd.google-apps.folder',
- ));
- // Return the created folder's id
- return $createdFile->id;
- } catch (Exception $e) {
- //print "An error occurred: " . $e->getMessage();
- print_r($e);
- }
- }
- }
- function upload(){
- $client = $this->client;
- $service = new Google_Service_Drive($client);
- $folderName=rand();
- $folderDesc='Folder Desc';
- //$client->setDefer(false);
- // Setup the folder you want the file in, if it is wanted in a folder
- if(isset($folderName)) {
- if(!empty($folderName)) {
- $parent = new Google_Service_Drive_ParentReference();
- $parent->setId($this->getFolderExistsCreate($service, $folderName, $folderDesc));
- $file = new Google_Service_Drive_DriveFile();
- $file->title = "a.csv";
- $chunkSizeBytes = 1 * 1024 * 1024;
- $file->setParents(array($parent));
- }
- }
- //$client->setDefer(true);
- $fileRequest = $this->fileRequest;
- $mimeType = $this->mimeType;
- $request = $service->files->insert($file,array('mimeType'=> 'binary/octet-stream'));
- // Create a media file upload to represent our upload process.
- $media = new Google_Http_MediaFileUpload(
- $client,
- $request,
- $mimeType,
- null,
- true,
- $chunkSizeBytes
- );
- $media->setFileSize(filesize($fileRequest));
- //$request = $this->google_drive->files->insert($file, array('mimeType'=> 'binary/octet-stream', 'mediaUpload' => $media));
- // Upload the various chunks. $status will be false until the process is
- // complete.
- $status = false;
- $handle = fopen($fileRequest, "rb");
- // start uploading
- //echo "Uploading: " . $this->filename . "\n";
- $filesize = filesize($fileRequest);
- // while not reached the end of file marker keep looping and uploading chunks
- while (!$status && !feof($handle)) {
- $chunk = fread($handle, $chunkSizeBytes);
- $status = $media->nextChunk($chunk);
- }
- // The final value of $status will be the data from the API for the object
- // that has been uploaded.
- $result = false;
- if($status != false) {
- $result = $status;
- }
- fclose($handle);
- // Reset to the client to execute requests immediately in the future.
- $client->setDefer(false);
- ?>https://drive.google.com/open?id=<?= $result->id ?><?php
- }
- }
- ?>
Add Comment
Please, Sign In to add comment