【发布时间】:2022-12-15 02:22:49
【问题描述】:
问题是我得到的日志表明文件已创建并从 googleApi 上传,但实际上没有任何变化。下面我把我的上传图片的php代码:
<?php
include '../vendor/autoload.php';
function handleGoogleDrive($file)
{
//connecting to google drive
$client = new \Google_Client();
$client->setApplicationName('Somesite');
$client->setScopes([\Google_Service_Drive::DRIVE]);
$client->setAccessType('offline');
$client->setAuthConfig('./credentials.json');
$client->setClientId('2445617429-6k99ikago0s0jdh5q5k3o37de6lqtsd3.apps.googleusercontent.com');
$client->setClientSecret('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');
$service = new Google_Service_Drive($client);
//counting amount of files in folder, there is no real reason in doing that
//it is just a test of connecting
$folder_id = '1eQtNOJjlA2CalZYb90bEs34IaP6v9ZHM';
$options = [
'q' => "'" . $folder_id . "' in parents",
'fields' => 'files(id, name)'
];
//printing result
$results = $service->files->listFiles($options);
echo count($results->getFiles());
//trying to add file
$data = file_get_contents("../test.jpg");
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid(). '.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$new_file = $service->files->create($file, [
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart',
]);
print_r($new_file);
}
【问题讨论】:
-
我必须为我糟糕的英语水平道歉。不幸的是,我无法理解
but nothing actually changes。我可以问一下您当前问题的详细信息吗? -
@Tanaike,我的意思是该文件仍未上传到 Google 驱动器
-
@Tanaike,我也打印了我的新文件变量,一切看起来都很好,但 Google Drive 仍然是空的
-
谢谢你的回复。我认为不需要包括
'mimeType' => 'image/jpeg',。但是,我认为您的脚本可以将文件上传到 Google 云端硬盘。所以,我无法理解but Google Drive is still empty。我为此道歉。例如,当你更改示例文件时,情况是否会发生变化? -
你能做一个 file.list 并再次检查是否没有创建新文件吗?
标签: php google-api google-drive-api google-api-php-client