Uploading File using googledrive api in php

Question!

I have searched on how to upload files on googledriveapi using php.I used following method

$file-setMimetype='image/jpg';
$file->setTitle='abcd'
$service->files->insert($file).

It inserts file on googledrive with the title and mimetype but it is empty.My question is how to upload actual data or content contained in the file i.e. if I want to upload the image or pdf file from my system to googledrive.How do I.Please help me on how to send actual data in $data.What that $data variable should contain.

By : trim kaur


Answers

Try this:

$service->files->insert($file, array(
    'data' => $content,
    'mimeType' => $mimeType,        
));

You can take a look at this example of how to insert a file (Click to PHP tab). Try to upload a text/plain file with content first, image data may need to be encoded in base64 before you can add to content.

By : KiL


There has been an update to the api and the documentation has not change accoringly. So on Claudio comment above the
"$file = new Google_DriveFile();"
should change to
$file = new Google_Service_Drive_DriveFile();



This video can help you solving your question :)
By: admin