Problems with uploading images in PHP

Asked

Viewed 751 times

1

I never touched uploading files and today I really need to do it. I have a code to test, to then use it on the site.

Here’s the HTML:

<html>
<body>
<form action="inserir.php" method="POST" enctype="multipart/form-data">
    <label>File: </label><input type="file" name="imagem" />
    <input type="submit" />
</form>
</body>
</html>

And here the PHP:

<?php

    $con = mysqli_connect("localhost","root","", "teste3");

    $image = addslashes(file_get_contents($_FILES['imagem'])); //SQL Injection defence!
    $image_name = addslashes($_FILES['imagem']);
    $sql = "INSERT INTO testeimg (imagem) VALUES ({$imagem})";

    if(mysqli_query($con, $sql)){
    echo "<script>alert('Sucesso');</script>";
}
else{
    echo mysqli_connect_error();
}

?>

I’ve been doing some research and this is what I did, but it makes me feel wrong to insert BD.

Can someone help me with this?

PS: I have a database: teste3, table: testeimg with id auto_increment PK and imagem with LONGBLOB.

  • 2

    I don’t advise you to save images directly in the comic book. You should have the images somewhere and in the comic book only the path for the image.

  • 1

    But if you want to keep it, you must use it $_FILES['imagem']['tmp_name'] to save the image to the BD.

  • @Jorgeb. Thank you for the suggestion! Can you show me a place to learn how to do that? I just don’t know (now I know a little but I’m still a turnip)

  • @Jorgeb. As I point out to the image go to an Upload folder

  • 1

    You can see the example in my reply.

3 answers

1

Try so, this way you will store the image in BD:

$nomeFinal = time().'.jpg'; //nome final que será trocado com o temporario
//move e renomeia o arquivo (se conseguir mover é porque o arquivo enviado é valido)
if (move_uploaded_file($imagem['tmp_name'], $nomeFinal)) { 
    $tamanhoImg = filesize($nomeFinal); 
    $mysqlImg = addslashes(fread(fopen($nomeFinal, "r"), $tamanhoImg));
    $con = mysqli_connect("localhost","root","", "teste3");
    mysqli_query($con,"INSERT INTO teste (imgem) VALUES ('$mysqlImg')");
    unlink($nomeFinal);// apaga a imagem do diretorio porque ela já esta 
    //armazenada no BD
}

NOTE: It is advisable not to store files in the BD but their address.

1

For some obscure reason in my web server I need to put [0] in tmp_name, because it returns me an array, in your case it may be necessary to remove it.

<?php
 $binario = file_get_contents($_FILES['files']['tmp_name'][0]);
 $lenght = $_FILES['files']['size'][0];
 if($lenght > 0){

   $con = mysqli_connect("localhost","root","", "teste3");
   $sql = "INSERT INTO testeimg (imagem) VALUES ('" . mysql_real_escape_string($binario,$con) . "')";

   if(mysqli_query($con, $sql)){
      echo "<script>alert('Sucesso');</script>";
   }else{
      echo mysqli_connect_error();
   }
}else{
    echo "Erro no Upload!";
}
?>

1


I don’t advise you to save images directly in the comic book. You should have the images somewhere and in the comic book only the path for the image.

To do this just use the function move_uploaded_file

$image    = $_FILES['imagem']['tmp_name'];
$img_name = $_FILES['imagem']['name'];

$dir      = "/caminho/para/a/pasta/";
$path     = $dir.$img_name;

move_uploaded_file( $image, $path );    

$sql = "INSERT INTO testeimg (imagem) VALUES ({$path})";
//não esquecer que imagem passa a ser um VARCHAR

But if you want to keep it, you must use it $_FILES['imagem']['tmp_name'] to save the image to the BD.

$image = $_FILES['imagem']['tmp_name'];
$sql   = "INSERT INTO testeimg (imagem) VALUES ({$image})";

Here is the official handbook and there are also examples Upload files with the POST method

  • If I want to put an img folder in the same folder as this file, which I put in $dir? img only?

  • 1

    $dir = "img/"; or $dir = "./img/";

  • And how do I get it? I’m sorry I’m asking so many questions but I’ve never touched upload files and I’m a bit of a jerk.. Just put in the src of the img type a variable to fetch the path of db?

  • Depends, what do you want to do with the image? Show it on the page or download it?

  • I wanted to show it. And if it’s with files it’s the same process?

  • 1

    I think you’d better ask a question with that, which is a little big for comments. But basically you search the path stored in the comic book and use it in HTML <img src="<?php echo "$dir/$img_name" ?>" > or all in PHP echo " <img src='$dir/$img_name' >";

Show 2 more comments

Browser other questions tagged

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