Google Drive API upload 1 fiile from php

Viewed 78

on output when open page -

File ID: 1qG8tteyVhAbB_rbu_VUvaE9ReqnSjEAh...

But on google drive no new files are created. I want upload file to cron but now i want only download test.pdf and end.

require_once './google-api-php-client/vendor/autoload.php';
use Google\Client;
use Google\Service\Drive;

function uploadBasic()
{
    try {
        $client = new Client();
        //$client->useApplicationDefaultCredentials();
        $client->setAuthConfig('./google-api-php-client/1710-6c50418be6b2.json');
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);
        
        $fileMetadata = new Drive\DriveFile(array(
                'parents' => ['225qhcKKyf8Ot0IhrRxRtqgHNTxLV1LiyI'],
                'name' => 'test.pdf',
                'mimeType' => 'application/pdf'));
        
        $mimeType=mime_content_type($fileMetadata);
        $content = file_get_contents('https://example.com/test.pdf');
        $file = $driveService->files->create($fileMetadata, array([
                    'data' => $content,
                    'mimeType' => 'application/pdf',
                    'uploadType' => 'multipart',
                    'fields' => 'id']));
        printf("File ID: %s\n", $file->id);
        return $file->id;
    } catch(Exception $e) {
            echo "Error Message: ".$e;
    }   
}

uploadBasic();
1 Answers

how to debug issue

The fastest way to debug this is to do a File.list. This will tell you if in fact the file was uploaded.

You are not setting parents yin your meta data, so the file will have been uploaded to the root directory.

service account

Remember if you are using a service account that the files are uploaded into the service accounts google drive account, not your personal drive account.

To upload to your personal drive account you would need to create a directory on your drive account, share that directory with your service account using the service account email address. The service account email address can be found in the json key file its the only one with an @.

Then set parents in the meta data to the folder on your drive account

$fileMetadata = new Drive\DriveFile(array(
                'parents' => { 'FolderId' }
                'name' => 'ASB-Background-3.png'));

File 0 size error after edit

You edited your question. It originally stated you were doing this

  $content = file_get_contents('./google-api-php-client/ASB-Background-3.png');

It is bad practice to update your question and change your code. It changes the answer to your question and in this case your error message.

That being said From the documentation for file_get_contents

file_get_contents — Reads entire file into a string

There is nothing in the documentation that states that this method could load a file from a url. So your edit changing to a URL is probably not going to work.

file_get_contents('https://example.com/test.pdf');

Your file is uploading with 0 because your not giving it a file. Download that file on to the machine running it and then send it, or write our own method which will accept a url and return a string file conents.

upload image

Files are uploaded in two parts first the fileMetadata and then the file itself.

MimeType must be properly set to that of the file you are uploading. file_get_contents will only work on a file that is currently accessible by your code.

If the file size is 0 make sure

  1. to check the most recent uploaded file. every create will create a new file.
  2. ensure that your code has access to the file you are uploading.
  3. make sure the mimeType is correct.

Sample.

try {
    $client = new Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Drive::DRIVE);
    $driveService = new Drive($client);
    $fileMetadata = new Drive\DriveFile(array(
    'name' => 'photo.jpg'));
    $content = file_get_contents('../files/photo.jpg');
    $file = $driveService->files->create($fileMetadata, array([
        'data' => $content,
        'mimeType' => 'image/jpeg',
        'uploadType' => 'multipart',
        'fields' => 'id']));
    printf("File ID: %s\n", $file->id);
    return $file->id;
} catch(Exception $e) {
    echo "Error Message: ".$e;
} 
Related