0

I have to integrate Google drive API in my CodeIgniter website, I have used CodeIgniter file upload form to get user file, now I want this file to be uploaded in google drive. I tried with the below script it is working great as core PHP but when I try to use it for CodeIgniter I am getting error Like this:

//This is a link from where I got the code Google Drive PHP API - Simple File Upload

  //Insert a file
            $file = new Google_Service_Drive_DriveFile();
            $file->setName(uniqid().'.jpeg');
            $file->setDescription('A test document');
            $file->setMimeType('image/jpeg');
//to get file uploaded by user 
            $data = file_get_contents($_FILES['userfile']['tmp_name']);

//This function is returning an error
            $createdFile = $service->files->create($file, array(
                  'data' => $data,
                  'mimeType' => 'image/jpeg',
                  'uploadType' => 'multipart'
                ));

Severity: Warning

Message: count(): Parameter must be an array or an object that implements Countable

Filename: Handler/CurlFactory.php

what should I enter as argument in $service->files->create()

enter image description here enter image description here

My complete code

enter code here

public function do_upload(){
            $config['upload_path']          = './assets/';
            $config['allowed_types']        = 'gif|jpg|png|jpeg';


                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('userfile'))
                {
                        $error = array('error' => $this->upload->display_errors());
                         print_r($error);
                        //$this->load->view('upload_form', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());
                         print_r($data);
                        //$this->load->view('upload_success', $data);
                }


        $client = new Google_Client();
        // Get your credentials from the console
        $client->setClientId('xxxx');
        $client->setClientSecret('xxxx');
        $client->setRedirectUri('http://localhost:8080/CodeIgniter/Google');
        $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

        session_start();

        if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
            if (isset($_GET['code'])) {
                $client->authenticate($_GET['code']);
                $_SESSION['access_token'] = $client->getAccessToken();
            } else
                $client->setAccessToken($_SESSION['access_token']);

            $service = new Google_Service_Drive($client);




            //Insert a file
            $file = new Google_Service_Drive_DriveFile();
            $fileMetadata = new Google_Service_Drive_DriveFile(array(
            'name' => 'photo.jpg'));

$content = file_get_contents($_FILES['userfile']['tmp_name']);

        $file = $service->files->create($fileMetadata, array(
        'data' => $content,
        'mimeType' => 'image/jpeg',
        'uploadType' => 'multipart',
        'fields' => 'id'));
        printf("File ID: %s\n", $file->id);



    } else {
        $authUrl = $client->createAuthUrl();
        header('Location: ' . $authUrl);
        exit();
    }
}
Alex
  • 9,215
  • 8
  • 39
  • 82
Ashutosh Eve
  • 143
  • 1
  • 4
  • 13
  • What version of the library are you using? – atymic Jul 23 '19 at 05:29
  • I have installed using composer, it is "google/apiclient": "2.0" – Ashutosh Eve Jul 23 '19 at 05:32
  • I think your file must be uploaded to drive . Have you checked it ? Your message is just warning , not exception error ... – hs-dev2 MR Jul 23 '19 at 05:36
  • @hs-dev2MR complete snap of Warnings and exception: Though Credential's can't as this script with same credential is working as core php, and The file's have not been uploaded to google drive – Ashutosh Eve Jul 23 '19 at 05:49
  • seems like your issue is more with authentication. please post the relevant parts of your code where you authorize the client and pass the client to the service class e.g. the "auth" part. – Alex Jul 23 '19 at 08:23
  • @Alex I have edited my question with complete code, after debugging I have found probable error may be due to difference of Format of Data that is being uploaded, as earlier in Core we just have to give the file path and it uploads that particular file, where as here in codeIgniter I have receiving file from upload form that is in encrypted format, But I don't know what should I give instead of *$_FILES['userfile']['tmp_name']* – Ashutosh Eve Jul 23 '19 at 08:46
  • i dont think that is it. i've recently started a project using gdrive. and the api isn't fun to work with. basically there are 2 methods for gaining access, 1 is using oauth which requires a user to verify the usage of their/your account (google oauth consent screen). seeing as your method directly uploads a file and then uploads it to gdrive that way won't work (the way you are currently doing it). you'd have to create a service account with allows you to authorize without a consent screen (this account typically is initialized with a .json key). the problem with this method is that – Alex Jul 23 '19 at 08:58
  • it really only works with gsuite not a personal google drive account. i'm not 100% sure if you need drive, but i would suggest dropbox. their api is built for this sort of thing for personal accounts. – Alex Jul 23 '19 at 09:00
  • just came to mind that you may be authorizing with a consent before uploading? is this the case? have you seen a consent screen? – Alex Jul 23 '19 at 09:02
  • Yes I have done Oauth if(check if access token exists){ is doing the same} https://stackoverflow.com/questions/25707891/google-drive-php-api-simple-file-upload try to run this code this actually works, Here when the script run's it redirects to get access token for Oauth then it after getting token it is redirected back to the redirected address, I have did the same. can you Please share code how you did for your gsuite – Ashutosh Eve Jul 23 '19 at 09:04
  • it is late here. i will investigate this later tomorrow and get back to you if i have a solution. also, in the future, don't forget to remove your app secret and other info as it is a direct path to your acct. i did it for you, but some might not. – Alex Jul 23 '19 at 09:07

0 Answers0