-3
<?php 
  if($row['top_pic'] == ""){
    echo "<img src='images/img.png' class='imimg1'>";
    } else {
    echo "<img src='images/".$row['top_pic']."' class='imimg1'>";
    } 
?>

Needs to change this to if (this file an image) {show it} else {show video}

tadman
  • 208,517
  • 23
  • 234
  • 262
D2rkZ3r0
  • 15
  • 4
  • 1
    How do you plan to check if the file is an image or video? by using the file extension? If so, this may be dangerous because it can be easily spoofed, check the `MIME` type instead. Your question isn't clear, you may want to rephrase it. – Pedro Lobito Apr 22 '17 at 20:40
  • Do try and keep your indentation under control. This style you've got here is highly misleading. You can also massively simplify this with `$row['top_pic'] || 'img.png'` instead of having two 80% similar chunks of code. – tadman Apr 22 '17 at 21:40

2 Answers2

0

You can get the file extension with this :

$file_extension = substr(strrchr($file_name ,'.'),1);

And then make some switch:

switch ($file_extension ) {
    case "mp4":
        echo "You have a video";
        break;
    case "png":
        echo "You have an image!";
        break;
    case "jpg":
        echo "You have an image!";
        break;
    default:
        echo "You have an unsupported file extension ";
}

Or just simply

if ($file_extension == "png") {

echo "<img src="">"
} else {
<video></video>
}
getl0st
  • 342
  • 1
  • 10
  • yes, yes, but i'd prefer checking for the mime type to prevent badly named file extension to being seens 'as' an image. Maybe OP is more confortable with your answer. – Louis Loudog Trottier Apr 22 '17 at 20:53
  • 1
    Judging by Ops code/question , I dont think that he knows what mime types are, and this code might be more confortable for him/her. – getl0st Apr 22 '17 at 20:54
0

you can use http://php.net/manual/en/function.mime-content-type.php to get the file mime type and check if it's an image. Make an array of valid mimes and check if the file mime is in this array: something like

//File name
$file='images/'.$row['top_pic'];

//allowed image mime type
$imagesMimes=['image/gif','image/jpg','image/png','image/jpeg']; 

//if the file exist and is allowed
if(file_exist($file) && in_array(mime_content_type($file),$imagesMimes)){
   //do your magic
}

PS: if you see something about the function being deprecated, see this thread: Why is mime_content_type() deprecated in PHP?

untested, just on top of my head.

Community
  • 1
  • 1
Louis Loudog Trottier
  • 1,367
  • 13
  • 26