【问题标题】:How to check image extension如何检查图像扩展名
【发布时间】:2017-03-08 01:46:31
【问题描述】:
$image= addslashes(file_get_contents(basename($_FILES['image']['tmp_name'])));
if(!empty($_FILES['image']['tmp_name']) && file_exists($_FILES['image']['tmp_name'])) //image is selected
{
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    if($ext == 'png' ||$ext == 'jpeg'||$ext == 'jpg')
    {
        //$insertQuery 
    }
    else
    {
        echo '<script language="javascript">';
        echo 'alert("Please select correct picture format")';
        echo '</script>';
    }

问题是我无法获取/验证文件扩展名。每次我上传符合条件的照片时,它只会显示“选择正确的图片”消息。

我的代码有什么问题?

【问题讨论】:

  • 调试:echo $ext
  • 还要考虑区分大小写
  • @ngod 什么意思?
  • @RiggsFolly 我已经尝试了所有的字母表
  • $filename从哪里来

标签: javascript php file


【解决方案1】:

你可以使用php内置的pathinfo函数。

<?php
$supported_image = array(
    'gif',
    'jpg',
    'jpeg',
    'png'
);

$src_file_name = 'abskwlfd.PNG';
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION)); // Using strtolower to overcome case sensitive
if (in_array($ext, $supported_image)) {
    echo "it's image";
} else {
    echo 'not image';
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2019-02-14
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多