php - 如何在 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(); } }
解决方案
推荐阅读
- matomo - Matomo 没有显示标签管理器
- symfony - 如何使用 Symfony YAML 配置配置二进制环境变量?
- google-apps-script - 有没有办法从 Google App Script 分配触发器所有权?
- python - /company-registration/ 处的 Django AttributeError
- swift - 类型 '() -> ()' 不能符合 'View';只有结构/枚举/类类型可以符合协议
- erlang - 如何使用 ejabberd hook muc_process_iq 获取 IQ 的子元素
- javascript - 角色职位:Discord.js
- snowflake-cloud-data-platform - 查询应用了数据屏蔽的列
- python - 将编辑写入文件会删除内容而不是对其进行编辑
- python - 刽子手不会打印两个相同字母的单词