【发布时间】:2020-03-21 10:44:10
【问题描述】:
我正在做一个项目,我需要通过 Google Drive API 将 .xlsx 文件上传到我的 Google Drive。然后还要取回链接,以便我可以将其存储到 MySQL 数据库中。
上传部分完成。问题是,我怎样才能取回文件链接,以便使用 Google 表格对其进行编辑。我想我需要将自定义文件 ID 添加到我的文件中。我真的不知道在哪里添加它。我的代码如下:
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$filefullname=$_GET['accid'].' '.$_GET['name'].'.xlsx';
// UPLOADING OF FILE TO GOOGLE DRIVE////////////////////////////////////
//fopen('./client_sheets/'.$_GET['name'], "w");
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_Service_Drive_DriveFile();
$file_path = './client_sheets/'.$filefullname;
$mime_type = finfo_file($finfo, $file_path);
$file->setName($filefullname);
$file->setDescription('This file contains details about: '.$filefullname.' || Client of GT Security');
$file->setMimeType($mime_type);
$file->setName($filefullname);
$createdFile = $service->files->create(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type,
'fields' => 'id'
)
);
print 'File ID: %s' % $createdFile->getId();
更多信息:我使用 PhpSpreadsheet 创建 .xlsx 文件。请问有人可以为我添加 FileID 字段吗?另外,有人可以向我解释如何获取文件链接,以便我可以使用 Google 表格进行编辑吗?
【问题讨论】:
标签: php google-sheets google-drive-api phpspreadsheet