I’m working with the Google Drive API to upload files using PHP. Currently, my files are being uploaded to the root directory of my Google Drive, but I need them to go to a specific folder instead.
Here’s the code I’m using right now:
<?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';
$driveClient = new Google_Client();
$driveClient->setClientId('***');
$driveClient->setClientSecret('***');
$driveClient->setRedirectUri('http://***');
$driveClient->setScopes(array('https://www.googleapis.com/auth/drive'));
if(empty($_GET['code']))
{
$driveClient->authenticate();
}
$uploadFile = "movie.mp4";
$fileType = 'video/mp4';
$driveService = new Google_DriveService($driveClient);
$token = $driveClient->authenticate($_GET['code']);
$driveClient->setAccessToken($token);
$driveFile = new Google_DriveFile();
$driveFile->setTitle('uploaded_movie.mp4');
$driveFile->setDescription('Video file upload');
$driveFile->setMimeType($fileType);
$fileContent = file_get_contents($uploadFile);
$uploadedFile = $driveService->files->insert($driveFile, array(
'data' => $fileContent,
'mimeType' => $fileType,
));
print_r($uploadedFile);
echo "<br />";
?>
I’ve looked at several solutions on different forums but they didn’t work for me and I keep getting errors. How can I modify this code to upload files to a specific folder instead of the root directory?