-1

so i want to get the file extension from the img so if i upload a image i can use mutiple file exts but when i use this code to get the file ext it does not work

$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">';
            }
        }
    }
}
  • It is not clear from your question what you are asking, please review the guidelines on asking questions. This way you are more likely to get a response quickly if we can understand what you are trying to achieve. – Xander Nov 17 '17 at 12:30
  • https://stackoverflow.com/questions/14746518/get-image-extension Duplicate – Yogesh Chuahan Nov 17 '17 at 12:31
  • can you please update some images names???? – K.B Nov 17 '17 at 12:31
  • 1
    why the nested sql queries both querying the users table? You might find `pathinfo( $file,PATHINFO_EXTENSION ) ` useful – Professor Abronsius Nov 17 '17 at 12:32
  • If someone update his picture with different file extension than previous one, this code will fail $filename = "../assets/IMGS/profile/Avatar".$id.".*"; – nithinTa Nov 17 '17 at 12:33
  • You can actually write your SQL queries on one single query using `SQL JOIN` - Also its not 100 % clear whats not working here. Do you get any output? Do you get errors? – Twinfriends Nov 17 '17 at 12:37
  • you don't even need to do a join - it's one table being queried. – Professor Abronsius Nov 17 '17 at 12:38

1 Answers1

0

Try to get your filepath with image and store ina variable $filename then try this

<?php
$path_parts = pathinfo($filename);
$extension =  $path_parts['extension'];
?>
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26