![](/img/trans.png)
[英]PHP execution timeout while uploading a large file to Google Drive with API
[英]How To Set Custom File ID While Uploading a File To Google Drive Using Google Drive API? (PHP)
我正在開發一個項目,在該項目中我需要通過 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 表格對其進行編輯嗎?
在這種情況下,您可以使用 Google Drive 自動轉換功能。 當您在 google drive 中創建文件時,它會自動轉換為電子表格文件。 這樣您就可以通過電子表格服務打開它並獲取需要存儲在數據庫中的 url。
創建 .xlsx 並將其轉換為電子表格文件:
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $filefullname,
'mimeType' => 'application/vnd.google-apps.spreadsheet'));
$content = file_get_contents($file_path);
$file = $driveService->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'text/csv',
'uploadType' => 'multipart',
'fields' => 'id'));
獲取電子表格網址:
$spreadsheetId = $file->id;
$sheetService = new Google_Service_Sheets($client);
$response = $service->spreadsheets->get($spreadsheetId);
$sheetUrl = $response->getSpreadsheetUrl();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.