【发布时间】:2021-05-22 18:40:45
【问题描述】:
我正在从谷歌驱动器下载图像文件,有些是 png、jpeg 或 gif 文件。这些文件是通过谷歌表单上传的,然后我通过谷歌表单响应中的 url 获取文件 ID。 我想获得原始名称或其扩展名。 目前正在使用以下有效的代码
$response = $service->files->get($file_id, array(
'alt' => 'media')); //service is the google drive service. file id is drive file id
$upload_file_url = "/uploads/" . time() . "_image.jpg"; //assigning the file as .jpg
$outHandle = fopen("myFolder/" . $upload_file_url, "w+");
while (!$response->getBody()->eof()) {
fwrite($outHandle, $response->getBody()->read(1024));
}
fclose($outHandle);
以上工作,但有些图像不是 jpg。是否可以获取 $response 文件名或文件扩展名来处理非 jpg 文件并使用正确的扩展名/s 保存它们
【问题讨论】:
-
我知道这有点无关紧要,但我有一个客户在共享所有文件夹的谷歌驱动器帐户中上传图像。我怎么能像你在你的问题中那样得到这些图像?在执行您提到的代码之前,您能给我一个提示吗?什么是 $service,有什么帮助吗?谢谢
-
$service 是谷歌驱动服务。请参阅以下 github gist 和完整代码:gist.github.com/Geowan/05aebffd66505a1f7701541173d9bfa6
标签: php google-drive-api