Insert a default image if no

Asked

Viewed 51 times

0

I want to know if the file is empty or not and if you do not insert a default image on the variable before inserting

This is what I have so far, I have also a part of type checking the type of session

 <form role="form" method="post" enctype="multipart/form-data">
<input type="file" name="inpFile" id="inpFile" class="inputfile"  />

<button type="submit" id="insert" name="insert"> Editar </button>
</form>
if (isset($_POST["insert"])) {
 $file = addslashes(file_get_contents($_FILES["inpFile"]["tmp_name"]));
 $username = trim($_REQUEST["username"]);
//Check if image is empty
 if(empty($file)){
   if($type == 1 || $type == 2 || $type == 3 || $type == 4){
     //Set $file a predefined image 
   }
 }
 $sql = "INSERT INTO pratos (name, image) VALUES ('$username', '$file')";
 $pdo->exec($sql);

}

This was the attempt I made trying to insert a default value

$file = "../img/prato_default.png";

I want to find a way to insert a default image and show it on another page

$pesquisa = "SELECT * FROM pratos";
$resultado_pesquisa = mysqli_query($conn, $pesquisa);

<?php while ($rows_pesquisas = mysqli_fetch_array($resultado_pesquisa)) { ?>
<img <?php echo 'src="data:image/jpeg;base64,' . base64_encode($rows_pesquisas['imagem']) . '"'; ?> class="image">
  • Face is not with PHP, but with pure CSS, and you might be interested https://answall.com/questions/321138/comort-do-um-stylingfor-imagem-broken-when-a-imagem-n%C3%a3o-load

1 answer

0


You need to check that the file was uploaded if you do not set the default value:

if (isset($_POST["insert"])) {
    if(isset($_FILES['inpFile'])) $file = addslashes(file_get_contents($_FILES["inpFile"]["tmp_name"]));
    else $file = "../img/prato_default.png";
...
  • And then how do I get it from the database and show it? When tetei showed no image even though it sent the path to the database

  • <img src="<? php echo $pathDaImage ? >" />, if not shown, is saving the wrong path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.