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

问题描述 投票:1回答:3

我通过使用以下代码行从我的PHP服务器上检索图像放置我的PHP服务器API

    $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)
php google-places-api
3个回答
0
投票

尝试pathinfo

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

它应该会帮助你

[编辑]由于您的图像以二进制形式返回,请使用php的getimagesizefromstring http://php.net/manual/en/function.getimagesizefromstring.php

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


1
投票

Google不会返回带后缀的文件。尝试使用mime_content_type($response),然后解析响应以找出正确的后缀


0
投票

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

$extension = pathinfo($response, PATHINFO_EXTENSION);
© www.soinside.com 2019 - 2024. All rights reserved.