Php 确定缺少扩展名的图像的文件扩展名

Php 确定缺少扩展名的图像的文件扩展名,php,image,file-extension,Php,Image,File Extension,我的系统中有一些图像是在没有文件扩展名的情况下保存的,它们以“FILENAME”的格式显示。(注意句号) 我正在尝试使用getimagesize();但它告诉我“文件名不能为空”时出错了 下面是我正在使用的代码示例 $contents = file_get_contents($localFile); $imageData = getimagesize($contents); // $imageData[2] will contain the value of one o

我的系统中有一些图像是在没有文件扩展名的情况下保存的,它们以“FILENAME”的格式显示。(注意句号)

我正在尝试使用getimagesize();但它告诉我“文件名不能为空”时出错了

下面是我正在使用的代码示例

    $contents = file_get_contents($localFile);

    $imageData = getimagesize($contents);
    // $imageData[2] will contain the value of one of the constants
    $mimeType = image_type_to_mime_type($imageData[2]);
    $extension = image_type_to_extension($imageData[2]);

需要一个文件名作为第一个参数查看文档:
getimagesize()需要一个文件名作为第一个参数,而不是图像文件的内容

尝试:

$imageData = getimagesize($localFile);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);

您的图像应该有扩展名(例如GIF、JPG、PNG等)。此外,并非所有浏览器都会将MIME类型返回到PHP脚本,因此最好创建一个允许扩展名的数组(例如$allowedExts=array('jpg','gif','png'),然后分解图像的文件名(例如$tmp=explode(“.”,$filename)),并使用_数组($tmp,$allowedExts)中的
检查上传的文件是否为图像
。如果是这样的话,你想用它做什么就做什么。