Skip to content
Advertisement

Drive API uploaded file always has zero size with PHP client

I’m trying to upload files into a google drive in a specific folder using a service account. It all appears to work – but the files always show up with 0 size. I’ve found a couple of very similar questions on here but none of the answers have worked.

The code I’m using is:

putenv('GOOGLE_APPLICATION_CREDENTIALS = ' . $home_path . '../google-oauth-credentials.json');


try {
    $gclient = new GoogleClient();
    $gclient->useApplicationDefaultCredentials();
    $gclient->addScope('https://www.googleapis.com/auth/drive');
    $driveService = new GoogleServiceDrive($gclient);

    $content = file_get_contents('testfile.txt');

    $fileMetadata = new GoogleServiceDriveDriveFile(array(
        'name' => 'test.txt',
        'parents' => array('{PARENT_ID}'),
        'description' => 'Test Description'
    ));

    $file = $driveService->files->create($fileMetadata, array([
        'data' => $content,
        'mimeType' => 'text/plain',
        'uploadType' => 'multipart',
        'fields' => 'id'
    ]));

    printf("File ID: %sn", $file->id);
    echo '<br>';
    echo '<pre>' . print_r($file,1) . '</pre>';
    echo '<h1>Content:</h1>';
    echo ($content);

} catch(Exception $e) {
    echo "Error Message: ".$e;
};

With {PARENT_ID} being the ID of the folder the files should be saved in.

This is meant to be uploading pdf files but I’ve switched to a simple text file to rule out a mime issue. With the PDF’s I was using “application/pdf” as the mimeType, I’ve also tried “application/octet-stream” for both pdf and text and I’ve tried putting the mimeType in the $fileMetaData as well as in the request body as it currently is.

I’ve tried using ‘uploadType’ of ‘media’ as well as some other answers had suggested.

I’ve also tried with and without the ‘fields’ => ‘id’ whose purpose I can’t seem to find in the API docs – but which was shown in various examples and other answers. I get the same results with or without it.

No luck. With any of that I get files with correct meta data….but they never have any content and are just 0 bytes in size.

I added the lines echoing out $content to confirm that file_get_contents is loading the correct file and its content to rule out a problem with what I’m passing to the request.

I’m loading the google api client through composer with

composer require google/apiclient:^2.12.1

And it seems to be working correctly as I can create google sheets, and edit their contents as well as create files…just the files always wind up having 0 size.

Advertisement

Answer

I found the problem.

The error was in:

    $file = $driveService->files->create($fileMetadata, array([
        'data' => $content,
        'mimeType' => 'text/plain',
        'uploadType' => 'multipart',
        'fields' => 'id'
    ]));

Which came more or less verbatim from: https://developers.google.com/drive/api/guides/manage-uploads

I’m also not sure why the ‘fields’ parameter is included since v3 create doesn’t appear to support that.

Note the “array([])” It’s defining an array inside the array.

Removing the [] or the array() solves the problem by correctly passing the intended array instead of passing an array containing the intended array.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement