在 PHP 中使用 file_get_contents() 时获取文件扩展名

Get file extension while using file_get_contents() in PHP

我正在从你那里检索图像 google 使用以下代码行 API 将 API 放在我的 PHP 服务器上

    $response = file_get_contents($url);

我也像这样将此文件写入服务器上的文件夹:(返回的文件是二进制文件,而不是图像文件)

    file_put_contents($folderPath,$response);

如何找出我读入 $response 的文件的文件扩展名?

我试过以下方法:

    $response = file_get_contents($url);
    $size = getimagesize($response);
    $extension  = image_type_to_extension($response)

尝试路径信息

http://php.net/manual/en/function.pathinfo.php

应该对你有帮助

[编辑] 由于您的图像是 return 二进制文件,因此请使用 php 的 getimagesizefromstring http://php.net/manual/en/function.getimagesizefromstring.php

它将 return 您需要的所有详细信息。

您可以使用 pathinfo 获取扩展名。

$extension = pathinfo($response, PATHINFO_EXTENSION);

Google 不会 return 带有后缀的文件。尝试 mime_content_type($response) 然后解析响应以找出正确的后缀