Upload alternate image in case of not having the correct image

0

I have this code that loads the player's image according to its name, is it possible to supplement this code by loading a default photo, when the code does not find the player's photo?

$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";
    
asked by anonymous 13.09.2016 / 00:39

2 answers

2

To run the code normally simply reset "$ player_img" if the reference does not point to an image on the server.

// receber a referencia noralmente
$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";
// verificar se não existe
if( !file_exists($player_img) ){
   // definir uma imagem padrão
   $player_img = "players/default.png";
}
    
13.09.2016 / 01:28
2

Answer:

$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";
if (file_exists($player_img)){
    echo '<img src="'.$player_img.'"></img>';
} else {
    echo '<img src="players/default.png"></img>';
}
    
13.09.2016 / 00:45