Problems uploading images in PHP

1

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

Here I have 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's 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 searching a bit and this is what gave me, but it gives me error inserting into the BD .

Can anyone help me with this?

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

    
asked by anonymous 10.07.2015 / 13:11

3 answers

1

I do not advise you to store images directly in the DB. You should have the images somewhere and in the DB just the path for the image.

To do this, simply use the 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 wanting to save you should use $_FILES['imagem']['tmp_name'] to save the image in the DB.

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

You have the official manual here and also have examples Uploading files with the method POST

    
10.07.2015 / 14:32
1

For some obscure reason on my web server I need to put [0] in tmp_name, because it returns a vector, in your case you may need 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!";
}
?>
    
10.07.2015 / 13:46
1

Try this way, you will store the image in the 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 DB but rather the address of them.

    
10.07.2015 / 13:23