php - upload file to Google drive through html form and store url in input-text box to write to mysql? -
how upload file through html form either file upload or google file picker , storing fil url mysql database ?
i tried upload file through file upload , passing $file variable in below code, file uploading invalid in google drive ?
<?php require_once 'google-api-php-client/src/google_client.php'; require_once 'google-api-php-client/src/contrib/google_driveservice.php'; $myfile=$_request['file']; $type=mime_content_type($myfile); $client = new google_client(); // credentials apis console $client->setclientid(''); $client->setclientsecret(''); $client->setredirecturi(''); $client->setscopes(array('https://www.googleapis.com/auth/drive')); $service = new google_driveservice($client); $authurl = $client->createauthurl(); //request authorization print "please visit:\n$authurl\n\n"; print "please enter auth code:\n"; $authcode = trim(fgets(stdin)); // exchange authorization code access token $accesstoken = $client->authenticate($authcode); $client->setaccesstoken($accesstoken); //insert file $file = new google_drivefile(); $file->settitle('my new document'); $file->setdescription('a test document'); $file->setmimetype($type); $data = file_get_contents($myfile); $createdfile = $service->files->insert($file, array( 'data' => $data, 'mimetype' => $type, )); print_r($createdfile); ?>
use $myfile=$_files['file']['tmp_name'];
instead of $myfile=$_request['file'];
you use 'trim(fgets(stdin));' won't work html page.
your app have read authcode $_get['code']
when google redirect authorized user site.
you can test code shown below. test have save code index.php
, make available on http://localhost/
. setredirecturi
allow redirect main domain (http://localhost/test.php
not allowed). when $_get['code']
empty redirected accounts.google.com. after grant app send http://localhost/?code={your authcode}
. can save authcode use on other pages too.
in test code file upload form submit http://localhost/?code={your authcode}
cause action attribute empty.
print_r($createdfile);
give information store file url in database.
don't forget setup google api console project credentials first, see http://enarion.net/programming/php/google-client-api/google-client-api-php/
<?php error_reporting(e_all); ini_set('display_errors', 1); require_once 'google-api-php-client/src/google_client.php'; require_once 'google-api-php-client/src/contrib/google_driveservice.php'; $client = new google_client(); // credentials apis console $client->setclientid('**********.apps.googleusercontent.com'); $client->setclientsecret('********************'); $client->setredirecturi('http://localhost/'); $client->setscopes(array('https://www.googleapis.com/auth/drive')); if(empty($_get['code'])) { $client->authenticate(); } ?> <form method="post" enctype="multipart/form-data" action=""> <input type="file" name="file"> <input type="submit" value="verzenden"> </form> <? if(!empty($_files['file']['tmp_name'])) { $myfile=$_files['file']['tmp_name']; $type=mime_content_type($myfile); $service = new google_driveservice($client); // exchange authorization code access token $accesstoken = $client->authenticate($_get['code']); $client->setaccesstoken($accesstoken); //insert file $file = new google_drivefile(); $file->settitle('my new document'); $file->setdescription('a test document'); $file->setmimetype($type); $data = file_get_contents($myfile); $createdfile = $service->files->insert($file, array( 'data' => $data, 'mimetype' => $type, )); print_r($createdfile); }
Comments
Post a Comment