php - 上传 Google Drive 文件 - PNG、JPEG、PDF
问题描述
我目前有一个功能可以成功将 jpg 文件上传到谷歌驱动器。我想知道如何修改该功能,以便我也可以将 pdf、jpg、png 和 docx 文件上传到谷歌驱动器。这是我的代码:
function uploadFiles($filePath) {
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents($filePath);
$createdFile = $this->service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
}
任何帮助表示赞赏。谢谢。
解决方案
在文件pdf, jpg, png, and docx
上传为的情况下pdf, jpg, png, and docx
,当使用 Drive API 中的 Files:create 方法上传文件时,似乎会自动检测到文件的 mimeType。我认为在您的情况下,可以使用这种情况。因此,请按如下方式修改您的脚本。
修改后的脚本:
function uploadFiles($filePath) {
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid());
$file->setDescription('A test document');
$data = file_get_contents($filePath);
$createdFile = $this->service->files->create($file, array(
'data' => $data,
'uploadType' => 'multipart'
));
}
- 通过上述修改,对于
pdf, jpg, png, and docx
,上传的文件具有正确的 mimeType。
推荐阅读
- elasticsearch - 弹性搜索索引上的多个分析器
- database - 将 Postgres DB 中的 2 个模式与不同的飞行路径合并
- javascript - 我需要一些帮助来验证 javascript 中的以下代码
- javascript - 需要帮助修改 skew.js 以进行 Y 轴旋转
- amazon-web-services - 在下面的 json 输出中使用 jq 时出现解析错误
- sql - 如何显示教师教 DBMS 系学生的系列表?
- python - 从元组列表中查找与用户输入对应的值
- google-bigquery - 数据流 SQL - 不支持的类型地理
- c# - MathNET.Numerics 中 Matlab unique() 的等效功能?
- windows - Wix:如何检索共享文件夹的本地化 Windows 组用户