首页 > 解决方案 > 如何在 CodeIgniter 的 Google Drive 中上传图片?

问题描述

我必须在我的 CodeIgniter 网站中集成 Google Drive API,我使用 CodeIgniter 文件上传表单来获取用户文件,现在我希望将此文件上传到 Google Drive。我尝试使用以下脚本,它作为核心 PHP 工作得很好,但是当我尝试将它用于 CodeIgniter 时,我收到如下错误:

//这是我获得代码的链接 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

我应该在 $service->files->create() 中输入什么作为参数

在此处输入图像描述 在此处输入图像描述

我的完整代码

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

标签: phpapicodeignitergoogle-api

解决方案


推荐阅读