How to Upload Files to Google Drive Using PHP
* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.
Integrating Google Drive into your web application for PHP file uploads can be a good option if you are just testing something out and don’t want to pay for extra hosting storage or you want users to save files to their own Google Drive account.
This tutorial will show you how to upload files to Google Drive using PHP, focusing on connecting to a Google Drive account and transferring files with ease.
Outline
- Introduction
- Why Use Google Drive for File Uploads?
- Setting Up Google Drive API
- Writing the PHP Code to Upload Files
- Creating an HTML Form for File Selection
- Connecting to Google Drive with PHP
- Uploading the File
- Conclusion
- Additional Resources
Introduction
Google Drive is a widely used cloud storage platform that provides secure and scalable solutions for storing files but it’s mainly used for personal file storage.
It has a developer-friendly API that you can use with PHP and it enables users to directly upload files to their Google Drive accounts.
In this guide, you’ll learn the basics of setting up the Google Drive API and using PHP to upload files efficiently.
Why Use Google Drive for File Uploads?
- Convenience: No need for self-hosted storage.
- Security: Google ensures robust data security and availability.
- Scalability: Unlimited file storage with enterprise accounts.
- Integration: Easy to access files across devices and applications.
Setting Up Google Drive API
Before you can upload files, you need to enable the Google Drive API and authenticate your app. Follow these steps:
Create a Google Cloud Project:
- Visit Google Cloud Console.
- Create a new project or select an existing one.
Enable Google Drive API:
- Navigate to APIs & Services > Library.
- Search for “Google Drive API” and enable it for your project.
Set Up OAuth 2.0 Credentials:
- Go to APIs & Services > Credentials.
- Click Create Credentials > OAuth 2.0 Client ID.
- Configure the consent screen and application type (e.g., Web Application).
- Save the generated credentials file as
credentials.json
.
Writing the PHP Code to Upload Files
1. Creating an HTML Form for File Selection
Start with a simple HTML form to allow users to select files for upload.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload to Google Drive</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label for="file">Choose a file to upload:</label>
<input type="file" name="file" id="file" required>
<button type="submit">Upload</button>
</form>
</body>
</html>
2. Connecting to Google Drive with PHP
Install the Google API Client Library for PHP via Composer:
composer require google/apiclient
Create the upload.php
script to handle file uploads.
upload.php
require 'vendor/autoload.php';
use Google\Client;
use Google\Service\Drive;
// Initialize Google Client
$client = new Client();
$client->setAuthConfig('credentials.json');
$client->addScope(Drive::DRIVE_FILE);
// Authenticate and Initialize Google Drive Service
$service = new Drive($client);
// Check if a file is uploaded
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$fileTmpPath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
// Upload file to Google Drive
$fileMetadata = new Drive\DriveFile([
'name' => $fileName
]);
$content = file_get_contents($fileTmpPath);
$uploadedFile = $service->files->create(
$fileMetadata,
[
'data' => $content,
'mimeType' => mime_content_type($fileTmpPath),
'uploadType' => 'multipart',
'fields' => 'id'
]
);
echo "File uploaded successfully. File ID: " . $uploadedFile->id;
} else {
echo "No file uploaded.";
}
Conclusion
Uploading files to Google Drive with PHP is a powerful way to enhance your applications by integrating cloud storage capabilities. With a basic understanding of the Google Drive API and PHP, you can implement a seamless file upload process that meets your users’ needs.