Insert image.jpg in the database

Asked

Viewed 351 times

3

Good morning!
I intend to insert an image into the database in the format . jpg, but when it is inserted it appears like this in the database: ???? JFIF
Here is my code for the image:

<form action="" method="POST" enctype="multipart/form-data">
   <input type="file" name="imagem" placeholder="Insira a imagem do produto" required/>
</form>
<?php
    $imagem_produto=addslashes(file_get_contents($_FILES['imagem']['tmp_name']));
    $inserir_produto=mysqli_query($link,"INSERT INTO produtos (imagem) VALUES('{$imagem_produto}')");
?>

What he wanted was for the name and extension of the image to appear in the database, for example "image.jpg" only this!

2 answers

3


If you just want the name, why don’t you use it like this?

$nome_img = $_FILES['imagem']['name'];
if(move_uploaded_file($_FILES['imagem']['tmp_name'], "images/".$nome_img)){
   $inserir=mysqli_query($db, "INSERT INTO coluna(nome) VALUES ('$nome_img')");
}else{
   echo "Erro!";
}

With this you will insert the image in the database.

I suppose you have a code for the upload but I leave it here anyway.

I point out that I usually use varchar(255) in the database.

  • The name and the extension! because without the extension will not be worth anything :/

  • With that he’ll get both

  • Yes I am also using varchar. I tried to implement your code, but it is a mistake , it does not say which... And by the way in your if "images/ would be the right folder name?

  • @Ana Yes but gives error in which line? You do the submit form?

  • It does not say which line is wrong, it appears the error that comes from echo "erro"; And yes I do form Ubmit

  • The path of that briefcase is right?

  • @Ana this means that she could not move the image that was loaded to the final folder, in this case images. Probably the path to the folder images is different.

  • The way to the briefcase in my case would be ../images/ and yes he is correct :/ I apologize for the inconvenience by the way...

  • But it’s working right?

  • Well, I appreciate all your help, it’s working at last!!

  • this code gives me error with ";" in query

  • @Joségomes as well as so?

  • when putting this code in my project it gives error in the query ;

  • was missing a ")" here -- if(move_uploaded_file($_FILES['image']['tmp_name'], "images/". $im_name){

  • 1

    @Thank you Joségomes! I will correct immediately

Show 10 more comments

1

The table field where your image will end up must be blob or mediumblob depending on the size you need! Then just show the Row on the side you need and it will appear!

Code to insert image

Make a button and make it Ubmit with name=""

if(isset($_POST['submit']))
{


 $name = $_FILES['imagem']['name'];
 $target_file = basename($_FILES["imagem"]["name"]);

 // Select file type
 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

 // Se quiseres só jpg deixa só jpg
  $extensions_arr = array("jpg","jpeg","png","gif");

 // Check extension
 if( in_array($imageFileType,$extensions_arr) ){

 // Convert to base64 
 $image_base64 = 
 base64_encode(file_get_contents($_FILES['imagem'] 
['tmp_name']) );
 $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;


$query="INSERT INTO produtos(imagem) VALUES ('".$image."')";
$query_run = mysqli_query($conn,$query);
  • Almostdone, thanks for the help, I did just like, and yes I had already created a button Ubmit, the problem is that now in the database appears data:image/jpg;base6 I wanted it to appear for example imagem.jpg

  • I’m sorry to ask but why do you need it so?

  • Because I’m making a virtual store, and to dent the products is for example: <img src="images/<?php echo $row['imagem']; ?>" alt=""> so to work in the database you should find only the name and extension of the file, because to show already I indicate the name of the folder, and so it is images/imagem.jpg

  • Why not just make a picture column of the mediumblob type and keep only <img src ="<? php echo $Row['image']; ? > and if you do as said above it will directly fetch the photo in the database!

  • Now I’m late, because I already have more than 200 products inserted, and I’ve all inserted by hand, I’ve practically finished the project!

  • You should have done a while loop to repeat the database records! So I don’t know how to help you

  • Yes I did a while cycle to run all the records!

  • Because the only problem in the change I proposed in the comments is that you would have to insert the 200 products all over again!

  • I prefer not to risk, because I cannot delete the whole table now, because I have several foreign keys etc

  • Yeah, obviously not good luck with that and I’m sorry I couldn’t help.

  • No problem, next time I’ll try it this way! Thank you

Show 6 more comments

Browser other questions tagged

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