how to upload a file through the html form either by file upload or Google file picker and storing the fil url to mysql database ?
i tried to upload file through file upload and passing it to the $file variable in the below code, the file is uploading but is invalid in the 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();
// Get your credentials from the 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 the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a 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));' this won't work from a html page.
Your app have to read your authcode from $_GET['code'] when google redirect the authorized user back to your site.
You can test this with the code shown below. To test it you have to save the code as index.php and make it available on http://localhost/.
setRedirectUri only allow to redirect to a main domain (http://localhost/test.php is not allowed).
When $_GET['code'] is empty you will be redirected to accounts.google.com.
After you grant the app you will be send back to http://localhost/?code={your authcode}.
Now you can save the authcode to use it on other pages too.
In this test code the file upload form will submit to http://localhost/?code={your authcode} cause the action attribute is empty.
print_r($createdFile); will give you the information to store the file url in your database.
Don't forget setup up an Google API console project to get your credentials first, see also 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();
// Get your credentials from the 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 for access token
$accessToken = $client->authenticate($_GET['code']);
$client->setAccessToken($accessToken);
//Insert a 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With