How to find an image that is stored in the database and display it in an HTML IMG?

Asked

Viewed 107 times

2

So, I am making a page on which the user uploads an image and it is stored in the database (Mysql). See below how it is stored in the bank:

inserir a descrição da imagem aqui

  • So far so good. Now I would like to take this image and display inside a IMG, in your src attribute, from my html page. See below:

inserir a descrição da imagem aqui

  • I would like to make a request via AJAX to return this image that is in the database and put it inside this IMG that I highlighted in the image above. (OBS: This photo shown in the image was placed directly in the src of an IMG and it is inside a folder).

  • If anyone has any suggestions on how to do this, I would appreciate it. No need to post codes and codes, it’s just a light on how to do this. (I’m using jQuery, Ajax, PHP and Mysql).

  • Thank you! (If you need any part of my code, to clarify the situation, just let us know. I did not ask to not leave the question too extensive).

Updating: I will post the part of the code into which I insert the image (along with user data) in the database table:

<?php

  function formularioPerfil($conecta)
	{	
				
		$usuario = $_SESSION['usuario'];
		$nome_completo = !empty($_POST["nome-completo"]) ? filter_input(INPUT_POST, 'nome-completo', FILTER_SANITIZE_STRING) : "";		
		$frase = !empty($_POST["frase"]) ? filter_input(INPUT_POST, 'frase', FILTER_SANITIZE_STRING) : "";			
		$email = !empty($_POST["email"]) ? filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING) : "";			
		$data_nascimento = !empty($_POST["data-nascimento"]) ? filter_input(INPUT_POST, 'data-nascimento', FILTER_SANITIZE_STRING) : "";		
		$escolaridade = !empty($_POST["escolaridade"]) ? filter_input(INPUT_POST, 'escolaridade', FILTER_SANITIZE_STRING) : "";			
		$cargo_atual = !empty($_POST["cargo-atual"]) ? filter_input(INPUT_POST, 'cargo-atual', FILTER_SANITIZE_STRING) : "";		
		$biografia = !empty($_POST["biografia"]) ? filter_input(INPUT_POST, 'biografia', FILTER_SANITIZE_STRING) : "";		
		
		$hoje = date('Y-m-d');				
		
		//SE HOUVER ARQUIVO, FAZ INSERÇÃO
		if(!empty($_FILES['imagem']['name']))
		{
			//UPLOAD DE ARQUIVO  VINDO DO FORMULÁRIO
			$files = $_FILES["imagem"];


			if($_FILES["imagem"]["error"][0] != 4)
			{
			
				$_verifica['tamanho'] = 1024 * 1024 * 2; // 2Mb	
				// Array com as extensões permitidas
				$_verifica['extensoes'] = array('jpg','jpeg','jpg','png');
				
						$file_tmp = $_FILES["imagem"]["tmp_name"]; //NOME DO ARQUIVO NO COMPUTADOR
						$file_name = $_FILES["imagem"]["name"];
						$file_size = $_FILES["imagem"]["size"]; 
						$file_type = $_FILES["imagem"]["type"];
						$partes = explode(".", $file_name);
						$extensao = end($partes);
						////verificando a extensão.
						//$extensao = strtolower(end(explode('.', $_FILES['upload-file']['name'])));
						if (array_search($extensao, $_verifica['extensoes']) === false) {
							die('extensaoerrada');
						}
						// Faz a verificação do tamanho do arquivo
						else if ($_verifica['tamanho'] < $file_size) {
							die('tamanhomaiorqueopermitido');
						}								
						
						//Nomeando o arquivo que será armazenado
						$nome_do_arquivo_final = $hoje . "-" . str_replace(' ','-', $usuario) . '.' . $extensao;
						
						
						$binario = file_get_contents($file_tmp); // evitamos erro de sintaxe do MySQL
						$binario = mysqli_real_escape_string($conecta, $binario);
						//FIM DO UPLOAD DE ARQUIVO							
			}
			$update_usuario = "UPDATE tbl_usuarios SET nome_completo = '$nome_completo', data_nascimento = '$data_nascimento', escolaridade = '$escolaridade', cargo_atual = '$cargo_atual', biografia = '$biografia', frase_usuario = '$frase', email = '$email', imagem = '$binario', nome_imagem = '$nome_do_arquivo_final', tipo_imagem = '$file_type' WHERE usuario = '$usuario' AND funcionario = '$usuario'";
		}
		else
		{		
			$update_usuario = "UPDATE tbl_usuarios SET nome_completo = '$nome_completo', data_nascimento = '$data_nascimento', escolaridade = '$escolaridade', cargo_atual = '$cargo_atual', biografia = '$biografia', frase_usuario = '$frase', email = '$email' WHERE usuario = '$usuario' AND funcionario = '$usuario'";
		}			
			
			
		$result_update = mysqli_query($conecta, $update_usuario);		
		
		if($result_update)
		{
			echo "dadosalteradoscomsucesso";
			die();
		}
		else
		{
			mysqli_error($conecta);
			echo "erronoupdatedousuario";
			die();
		}		
			
	}

?>

  • What do you mean Pending? What could I do to improve this question ? Only if maybe I post the whole code, which I think is unnecessary,.

  • 1

    See if this helps, https://answall.com/questions/142841/comor-display-uma-imagem-storageno-campo-blob-do-mysql-em-uma-página-php.

  • I think it should be closed by "duplicate" and not "out of scope".

  • If it is duplicate (really have an equal), I think close beauty. I searched, but only found using languages that I do not know. I’ll take a look at that link of yours.

  • @Inkeliz, told me that it is not right to store images in the database. You agree with this ?

  • As I understand it, the contents of the image are stored in the database. It is considered more appropriate to store the file path on the server. But you can use this way, no need to ajax, just set a php file as src of the image and in this file set a header to indicate that it is an image: header('Content-type: image/jpeg'); and echo the contents of the image from the database.

  • 1
  • Thanks, @Inkeliz and Icaro Martins. Your help was great. Your links solved my problem.

Show 3 more comments
No answers

Browser other questions tagged

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