I’m having trouble with file uploads using the PHP Google Drive API. Every time I try to upload a file, it shows up in Google Drive without a proper title.
I’m using the most recent version of the Drive API library from June 2012. The file content gets uploaded correctly, but something goes wrong during the upload process that causes the filename to be missing.
Here’s my current upload code:
$uploadedFile = $driveAPI->files->insert($fileObject, array('content' => $fileContent, 'contentType' => $fileType));
I noticed in the API service file there’s this configuration:
public function insert(FileResource $requestBody, $additionalParams = array()) {
$parameters = array('requestBody' => $requestBody);
$parameters = array_merge($parameters, $additionalParams);
$response = $this->__call('insert', array($parameters));
if ($this->useObjects()) {
return new FileResource($response);
} else {
return $response;
}
}
I’m passing content and contentType in the parameters, but I’m not sure if these are being handled properly by the API. Could this be related to my Google API Console settings or do I need to format the data differently before sending it?
same thing happened to me. double-check ur setting the name property right in ur fileObject before calling insert. api version might be the issue - older versions used setTitle() but newer ones use setName(). june 2012 is pretty old, so check which version ur running.
Your file metadata setup is the problem. You need to explicitly set the title property in your FileResource object before uploading. The content parameters you’re passing don’t affect the filename that Drive displays.
Try this:
$fileObject = new Google_Service_Drive_DriveFile();
$fileObject->setTitle('your-filename.ext');
$uploadedFile = $driveAPI->files->insert($fileObject, array('content' => $fileContent, 'contentType' => $fileType));
I hit this exact issue about a year ago and wasted hours debugging it. Drive’s API separates file content from metadata - your content uploads fine, but metadata fields like title have to be set explicitly on the FileResource object. Skip setting the title and Drive just creates a file with empty metadata.
You’re structuring the API call wrong. Don’t pass content and contentType directly in the parameters array when using files->insert. The API won’t recognize them that way for file uploads. Create a Google_Http_MediaFileUpload object first with your file content and MIME type. Then call insert with the file metadata object and media upload object separately. The Drive API needs this specific structure to handle both metadata and binary content. Also check that your file object is properly instantiated before setting properties. I’ve seen uploads succeed but lose metadata when the file object wasn’t created through the proper Google Drive service factory methods.