How to Upload Files to Google Drive Using PHP API Client

I’m working on a PHP script that uploads files from my local server to Google Drive using their API. I keep running into authentication issues and connection problems. Here’s what I have so far:

<?php

require_once 'vendor/autoload.php';

$googleClient = new Google_Client();
$googleClient->setScopes(['https://www.googleapis.com/auth/drive']);
$googleClient->setClientId('your_client_id_here');
$googleClient->setClientSecret('your_client_secret_here');
$googleClient->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');

$driveService = new Google_Service_Drive($googleClient);

$fileContent = file_get_contents('test_image.png');

try {
    $driveFile = new Google_Service_Drive_DriveFile();
    $driveFile->setName('uploaded_' + time() + '.png');
    
    $uploadedFile = $driveService->files->create($driveFile, array(
        'data' => $fileContent,
        'mimeType' => 'image/png',
        'uploadType' => 'multipart'
    ));
    
    echo 'File uploaded successfully: ' + $uploadedFile->id;
    
} catch (Exception $error) {
    echo 'Upload failed: ' + $error->getMessage();
}

?>

I’m getting connection errors and the upload never completes. The authentication seems to be the main problem. Can someone show me a working example of how to properly authenticate and upload files to Google Drive using PHP?

Google API authentication is a nightmare. I’ve fought this same issue tons of times at work.

Your code has problems. You’re using + for string concatenation instead of . in PHP. Plus your auth flow is broken - you’re not handling OAuth tokens right.

What you’re missing:

// Get the access token first
$authUrl = $googleClient->createAuthUrl();
// User visits this URL and gets the auth code
// Then swap it for tokens
$googleClient->authenticate($authCode);
$accessToken = $googleClient->getAccessToken();

But why torture yourself with this API mess? I used to waste hours on OAuth debugging and constant API changes.

I just automate file uploads through Latenode now. Set up a workflow that watches folders and auto-uploads to Google Drive. No PHP maintenance, no auth headaches.

Built one for our team last month. Files upload automatically, I can add steps like image resizing or date sorting. Takes 10 minutes vs hours of code debugging.

The visual builder shows exactly what’s happening, and Latenode handles Google’s API updates for you.

Check it out: https://latenode.com

you’re missing the token refresh part - that’ll bite you later. also check if your service account has write permissions on the Drive folder you’re uploading to. i spent 3 days debugging only to realize the service account couldn’t write to it lol. and make sure you’re using a recent Google client library version - older ones have bugs where file uploads just hang forever.

Ugh, went through this exact nightmare last year building a doc management system. Yeah, the other answer spotted your string concatenation bug, but you’ve got bigger problems. You’re completely missing the OAuth flow. You need to redirect users to Google’s auth URL, grab the code they send back, then swap it for access tokens. Your file upload params are wrong too - use ‘uploadType’ => ‘media’ for simple uploads and wrap your file data right. Here’s what’ll save your sanity: build token refresh logic now. Google tokens expire and your script just dies silently without it. Store the refresh token somewhere safe and auto-renew when needed. Honestly? If this is server-to-server uploads, ditch OAuth entirely. Use service account auth instead - way more reliable and no user interaction needed. Just generate a service account key file and use setAuthConfig() with the JSON path.