user3653474
user3653474

Reputation: 3852

Cannot upload file to Google Drive using php

I want to upload file to Google Drive But cannot upload it. I have Product Name : such as demo_google_drive_app_v1, client ID and client secret Key, I am using the following code but i'm getting error, I think i did'nt get the proper sdk to upload file to google drive i'm new to php and google drive and searched a lot for the answer but could not got it. Now i am getting following error message, I will be thank full if anybody helps me in solving my problem.

Fatal error: Class 'Config' not found in C:\wamp\www\upload_drive\Google-Drive-PHP-API....\src\Google\Client.php on line 80

<?php
/*
 * Simplified version of quickstart.php found on http://developers.google.com/drive/quickstart-php
 *
 */
require_once 'google-api-php/src/Google/Client.php';
require_once 'google-api-php/src/Google/Service.php';
$client = new Google_Client();


$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('XXXXXXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } else
        $client->setAccessToken($_SESSION['access_token']);

    $service = new Google_Service_Drive($client);

    //Insert a file
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents('a.jpg');

    $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => 'image/jpeg',
          'uploadType' => 'multipart'
        ));

    print_r($createdFile);

} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}

Upvotes: 1

Views: 1061

Answers (1)

Angel M.
Angel M.

Reputation: 2742

I think you should have at the top:

 require_once 'google-api-php-client/src/Google/autoload.php';

or add Config.php manually

require_once(path_to/Config.php)

check it here: https://github.com/google/google-api-php-client

simpler way would be to include autoload at the top, as it is suggested in installation instructions:

require_once 'google-api-php-client/src/Google/autoload.php';

Upvotes: 1

Related Questions