Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file to specific folder using google drive api with PHP

I am using google drive api to upload files, This is my code so far now the file upload to my root and I want to change the path to another folder,

   <?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        require_once 'google-api-php-client/src/Google_Client.php';
        require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
        $client = new Google_Client();
        // Get your credentials from the APIs Console
        $client->setClientId('***');
        $client->setClientSecret('***');
        $client->setRedirectUri('http://***');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));

        if(empty($_GET['code']))
        {
            $client->authenticate();
        }

        $myfile= "video.mp4";
        $type='video/mp4';

        $service = new Google_DriveService($client);

        // Exchange authorization code for access token
        $accessToken = $client->authenticate($_GET['code']);
        $client->setAccessToken($accessToken);


        //Insert a file

        $file = new Google_DriveFile();
        $file->setTitle('filename.mp4');
        $file->setDescription('A video');
        $file->setMimeType($type);

        $data = file_get_contents($myfile);

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

        print_r($createdFile);
        echo "<br />";

I tried few posts from stackoverflow and none of them worked for me I got error, Thanks in advance

like image 536
Ali Avatar asked Aug 21 '13 16:08

Ali


People also ask

How can upload file in specific folder in PHP?

Create The Upload File PHP Script$target_dir = "uploads/" - specifies the directory where the file is going to be placed. $target_file specifies the path of the file to be uploaded. $uploadOk=1 is not used yet (will be used later) $imageFileType holds the file extension of the file (in lower case)

Does Google Drive support PHP?

Authorizing your php script to connect to the google drive api is quite simple once you have the code. Using the PHP client library and allowing it to handle all of the authorization for us makes our life a lot easer. How to create Google Oauth2 installed application credentials.


2 Answers

It seems that the class Google_Service_Drive_ParentReference() is not working anymore. However, the following code works for me:

$fileMetadata = new Google_Service_Drive_DriveFile(array(
        //Set the Random Filename
        'name' => $rdmName,
        //Set the Parent Folder
        'parents' => array('0BzaRxkCDivjIM1pUYU1SLWVxOGM') // this is the folder id
    ));

    try{

        $newFile = $drive_service->files->create(
            $fileMetadata,
            array(
                'data' => file_get_contents(TESTFILE),
                'mimeType' => 'application/pdf',
                'uploadType' => 'media'
            )
        );


    } catch(Exception $e){

        echo 'An error ocurred : ' . $e->getMessage();

    }
like image 82
faniva Avatar answered Sep 22 '22 09:09

faniva


Set the parent folder like this:

//Set the Parent Folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId('0B9mYBlahBlahBlah');
$file->setParents(array($parent));
like image 21
thurzo101 Avatar answered Sep 20 '22 09:09

thurzo101