To manipulate a file, one must use the
$_FILES (http://php.net/manual/en/reserved.variables.files.php)
Saving images in the bank is by far not a good practice ! rs
As requested by the chat, a complete example of uploading the file to a directory and the file data in the database:
(take an image named "loading.gif" and leave it in the root folder, it will display it while the form sends)
*** Create an "attachments" folder in the root folder.
index php.
<html>
<head>
<link type="text/css" rel="stylesheet" href="../assets/css/materialize.min.css" media="screen,projection"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#formulario").submit(function() {
$('#formulario').hide();
$('.imagens').hide();
$('#gif').show();
//return true;
});
});
</script>
</head>
<body>
<img src="loading.gif" id="gif" height="auto" width="200" hidden>
<form id="formulario" method="post" enctype="multipart/form-data" action="_envio.php">
Selecione uma imagem:
<input name="arquivoX" type="file"/>
<br/>
<button type="submit">Salvar</button>
</form>
<?
date_default_timezone_set('America/Sao_Paulo');
$srv = "enderecoDoBanco";
$user = "usuarioDoBanco";
$pass = "senhaDoBanco";
$db = "nomeDoBanco";
$db = new mysqli($srv, $user, $pass, $db);
$sql = "SELECT * FROM anexos";
$res = $db -> query($sql);
while ($i = $res -> fetch_assoc()) {
$a[] = $i;
//echo "<pre>";
//print_r($i);
//echo $i['dir'].$i['arq'];
?>
<img class="imagens" src="<?echo 'anexos\\'.$i['arq']?>" height="60" width="60"/>
<?
}
?>
</body>
</html>
_php.
<?php
date_default_timezone_set('America/Sao_Paulo');
$srv = "enderecoDoBanco";
$user = "usuarioDoBanco";
$pass = "senhaDoBanco";
$db = "nomeDoBanco";
$db = new mysqli($srv, $user, $pass, $db);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
echo '<div style="background-color:green;color:white"><b>OK :: CONEXAO BD</b></div><hr>';
}
echo '<pre>';
print_r($_FILES);
print "</pre>";
$uploaddir = 'F:\Xampp\htdocs\_commands\files\anexos\\';
$uploadfile = time() . '-' . basename($_FILES['arquivoX']['name']);
if (move_uploaded_file($_FILES['arquivoX']['tmp_name'], $uploaddir.$uploadfile)) {
// Gera endereço da pasta para o mysql
$dir = str_replace('\\', '\\\\', $uploaddir);
// ******* TRATAR NOME (acentos, etc)
$arq = $uploadfile;
$extpat = pathinfo($_FILES['arquivoX']['name']);
$ext = $extpat['extension'];
echo '<div style="background-color:green;color:white">OK :: Arquivo válido e enviado com sucesso.<br></div>';
$db -> query("INSERT INTO anexos (`dir`,`arq`,`ext`) VALUES ('$dir','$arq','$ext')");
} else {
echo '<div style="background-color:orange;color:white">WARNING :: Possível ataque de upload de arquivo!<br></div>';
}
if ($db -> close()) {
echo '<div style="background-color:blue;color:white"><b>OK :: CONEXAO BD CLOSE</b></div><hr>';
} else {
echo '<div style="background-color:orange;color:white"><b>WARNING :: CONEXAO DB CLOSE</b></div><hr>';
}
sleep(3);
header('Location: index.php');
?>
How’s the
tipo
field in your database ?– rbz
So it’s really sweeping. I looked for a more suitable option but I couldn’t find,
– Mariana Bayonetta
The correct one would be "blob". Take a look here: http://www.devmedia.com.br/storage-imagens-no-mysql/32104
– rbz
@Raonibz The right thing would be
blob
? are sure?– Rafael Augusto
Your line of
SQL
is wrong, change to that$sql = "INSERT INTO imagens SET campo ='$image'
and to get the name of the image, you need to do so$image = $_FILES['imagem']['name']
– Rafael Augusto
@Rafaelaugusto as far as I know, yes, the blob ! Which would be ?
– rbz
@Rafaelaugusto
INSERT INTO imagens SET campo ='$image'
???– rbz
@Raonibz The best practice is to store only the file name in the database and the image in the server directory.
– Rafael Augusto
@Raonibz What is your doubt regarding my answer in the above comment?
– Rafael Augusto
@Rafaelaugusto for sure ! I have even ready example of this, but the question was not that.
– rbz
Why not values, if it is the most common ?
INSERT INTO imagens (imagem) VALUES ('$image')
– rbz
Let’s go continue this discussion in chat.
– rbz
It’s not about being ordinary or not, you don’t have
sintaxe
wrong, one is theSQL
and another ofMysql
, I gave an example of the way I use it, which doesn’t stop her from using it withvalues
, and still discover that it is possible withSET
– Rafael Augusto