How to specify destination directory when uploading files with Google Drive API in PHP

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?

Had the same problem building a file management system last year. You don’t need to mess with ParentReference objects - just pass the folder ID as a string in the parents array:

$driveFile = new Google_DriveFile();
$driveFile->setTitle('uploaded_movie.mp4');
$driveFile->setDescription('Video file upload');
$driveFile->setMimeType($fileType);
$driveFile->setParents(array('your_folder_id_here'));

Grab the folder ID from the Google Drive URL. Also watch out for folder permissions - your app needs write access to the target folder or you’ll get auth errors even if your code’s perfect.

Just add the folder ID directly after setting the mimetype: $driveFile->setParents(['folder_id_goes_here']); Grab the actual folder ID from the Drive URL, not the folder name. Works every time for me.

To upload files to a specific folder using the Google Drive API in PHP, you need to specify the parent folder’s ID in your DriveFile object. First, obtain the folder’s ID, which you can find in the URL when viewing the folder in Google Drive. Once you have the ID, modify your code as follows:

$driveFile = new Google_DriveFile();
$driveFile->setTitle('uploaded_movie.mp4');
$driveFile->setDescription('Video file upload');
$driveFile->setMimeType($fileType);

// Set the parent folder ID
$parent = new Google_ParentReference();
$parent->setId('your_folder_id_here');
$driveFile->setParents(array($parent));

This adjustment will direct the uploaded files into the designated folder instead of the root directory.