php从图像获取文件扩展名

时间:2017-11-17 12:28:56

标签: php

所以我想从img获取文件扩展名,所以如果我上传图片我可以使用多个文件exts但是当我使用此代码获取文件ext时它不起作用

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['user_id'];
        $sql = "SELECT * FROM users WHERE user_id='$id'";
        $result = mysqli_query($conn, $sql);
        while ($rowImg = mysqli_fetch_assoc($result)) {
            if ($rowImg['status'] == 0) {
                $filename = "../assets/IMGS/profile/Avatar".$id.".*";
                $fileinfo = glob($filename);
                $fileext = explode(".", $fileinfo[0]);
                $fileactualext = $fileext[1];

                echo "<img src='../assets/IMGS/profile/Avatar".$id.".".$fileactualext."?".mt_rand()."'>";
            } else {
                echo '<img src="../assets/IMGS/profile/def-image.png">';
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用图像获取文件路径并存储在变量$filename中,然后尝试此

<?php
$path_parts = pathinfo($filename);
$extension =  $path_parts['extension'];
?>