0

I'm building a simple chat box(no database, inputs are saved as txt file and images) for my website where users can share image and send text. Here's what I've came up so far:

   $log = glob(__DIR__.'/chatlog/*');
   foreach($log as $file){
      if(pathinfo($file, PATHINFO_EXTENSION) === "jpg" || "jpeg" || "png"){
            $name = basename($file);
            echo "<div><img style='width: 150px;' src='chatlog/$name'alt='failed to load'</div>";
      }
      
     if(pathinfo($file, PATHINFO_EXTENSION) === "txt"){
        $line = file($file);
        echo $line[0];
     }
  }

The problem is that in the first if, the text file is being included so it returns a broken image.

enter image description here

How can I exclude text file in the first if?

1 Answers1

0

I would do it like so

if((pathinfo($file, PATHINFO_EXTENSION) === "jpg" || "jpeg" || "png") && pathinfo($file, PATHINFO_EXTENSION) !== "txt"){
    $name = basename($file);
    echo "<div><img style='width: 150px;'src='chatlog/$name'alt='failed to load'</div>";
}
jnko
  • 163
  • 1
  • 12