How to upload files to a specific Google Drive folder using PHP API?

I’m working on a project where I need to upload files to a specific folder in Google Drive using PHP. Right now, my code uploads files to the root directory, but I want to change that. Here’s what I’ve got so far:

<?php
// API setup and authentication
$client = new GoogleClient();
$client->setCredentials('client_id', 'client_secret', 'redirect_uri');
$client->setScope('drive_scope');

// File details
$videoFile = 'my_video.mp4';
$fileType = 'video/mp4';

$driveService = new GoogleDriveService($client);

// Authenticate and get access token
$token = $client->getAccessToken('auth_code');
$client->setAccessToken($token);

// Create file metadata
$fileMetadata = new GoogleDriveFile();
$fileMetadata->setName('uploaded_video.mp4');
$fileMetadata->setDescription('Uploaded video file');
$fileMetadata->setMimeType($fileType);

// Read file content
$fileContent = file_get_contents($videoFile);

// Upload file
$uploadedFile = $driveService->files->create($fileMetadata, [
    'data' => $fileContent,
    'mimeType' => $fileType,
]);

dump($uploadedFile);

I’ve tried a few solutions from different posts, but none of them worked. How can I modify this code to upload the file to a specific folder instead of the root? Any help would be appreciated!

I’ve dealt with a similar issue in one of my projects. To upload files to a specific folder in Google Drive using PHP, you need to modify your file metadata. Here’s what worked for me:

  1. First, get the ID of the folder where you want to upload the file. You can find this in the URL when you open the folder in Google Drive.

  2. Then, add the folder ID to your file metadata like this:

$folderID = 'your_folder_id_here';
$fileMetadata->setParents([$folderID]);

Add this line right after you set the mime type in your existing code.

  1. The rest of your code can remain the same.

This should upload your file directly to the specified folder. Remember to replace ‘your_folder_id_here’ with the actual ID of your target folder.

Also, make sure you have the necessary permissions to write to that folder. If you’re still having issues, double-check your API credentials and scopes.

hey man, i kno the struggle! heres a quick fix:

just add this line after setMimeType:

$fileMetadata->setParents([‘your_folder_id’]);

replace ‘your_folder_id’ with the actual ID (find it in the folder URL).

thats it! should work like a charm now. goodluck with ur project!

I’ve encountered this challenge before and found a solution that works well. The key is modifying the file metadata to include the parent folder ID. Here’s what you need to do:

Obtain the folder ID where you want to upload. You can find this in the URL when viewing the folder in Google Drive.

Update your file metadata creation:

$fileMetadata = new Google_Service_Drive_DriveFile([
‘name’ => ‘uploaded_video.mp4’,
‘description’ => ‘Uploaded video file’,
‘mimeType’ => $fileType,
‘parents’ => [‘your_folder_id_here’]
]);

Replace ‘your_folder_id_here’ with the actual ID. This change tells the API to place the file in the specified folder rather than the root.

The rest of your code can remain as is. Make sure you have the necessary permissions for the target folder. If issues persist, verify your API credentials and scopes are correctly set up.