Hereâs a step-by-step guide on how to achieve directory and file interactions with Google Drive:
Step 1:Setting up Google Drive API
- Go to the Google Developer Console.
- Create a new project.
- Enable the Google Drive API for the project.
- Create credentials (OAuth 2.0 Client ID) for a Desktop app.
- Download the
credentials.json
file.
Step 2: Install Google Client Library
You can use Composer to install the Google API PHP client library:
composer require google/apiclient:^2.0
Step 3: Authenticating
Before you can use the API, you need to authenticate. Hereâs a basic script for that:
require 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('path_to/credentials.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setRedirectUri('http://your_redirect_uri');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive_service = new Google_Service_Drive($client);
// Your drive operations go here...
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
In oauth2callback.php
:
require 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('path_to/credentials.json');
$client->setRedirectUri('http://your_redirect_uri');
$client->addScope(Google_Service_Drive::DRIVE);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Step 4: Upload File to Google Drive
Hereâs a basic example to upload a file:
$file = new Google_Service_Drive_DriveFile();
$file->setName("filename.ext");
$file->setMimeType("type-of-file");
$data = file_get_contents('path_to/your_file.ext');
$createdFile = $drive_service->files->create($file, array(
'data' => $data,
'mimeType' => 'type-of-file',
'uploadType' => 'multipart'
));
Step 5: Download File from Google Drive
To download a file:
$fileId = "your-file-id";
$content = $drive_service->files->get($fileId, array("alt" => "media"));
file_put_contents('path_to/destination_filename.ext', $content->getBody());
Step 6: Handling Directories (Folders)
To create a folder:
$folder = new Google_Service_Drive_DriveFile();
$folder->setName('FolderName');
$folder->setMimeType('application/vnd.google-apps.folder');
$createdFolder = $drive_service->files->create($folder);
To list files in a folder:
$folderId = 'your-folder-id';
$results = $drive_service->files->listFiles(array(
'q' => "'$folderId' in parents"
));
foreach ($results->getFiles() as $file) {
echo $file->getName(), PHP_EOL;
}
Remember to handle exceptions and errors as needed. This is a basic guide, and depending on your needs, you might want to expand and refine these snippets.
Good Luck.