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()
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(); } }