Difficulty sending Image to the Database

0

I am having difficulty sending an image to my database.

Only the file name is going but File is not being Upgraded to DB.

Follow the Upload script:

if(isset($_POST['acao']) && $_POST['acao'] == 'editar'):
    $foto = $_FILES['foto'];
    $nome = strip_tags(filter_input(INPUT_POST, 'nome'));
    $sobrenome = strip_tags(filter_input(INPUT_POST, 'sobrenome'));
    $email = strip_tags(filter_input(INPUT_POST, 'email'));
    $usuario = strip_tags(filter_input(INPUT_POST, 'usuario'));
    $senha = strip_tags(filter_input(INPUT_POST, 'senha'));
    $descricao = htmlentities($_POST['descricao'], ENT_QUOTES);

    $val->set($nome, 'Nome')->obrigatorio();
    $val->set($sobrenome, 'Sobrenome')->obrigatorio();
    $val->set($email, 'Email')->isEmail();
    $val->set($usuario, 'Usuario')->obrigatorio();
    $val->set($senha, 'Senha')->obrigatorio();
    $val->set($descricao, 'Descricao')->obrigatorio();

    if(!$val->validar()){
        $erro = $val->getErro();
        echo '<div class="erros">Erro: '.$erro[0].'</div>';
    }elseif($foto['error'] == '4'){
        echo '<div class="erros">Informe uma imagem padrão!</div>';
    }else{
    $nomeImg = md5(uniqid(rand(), true)).$foto['name'];
    $painel->upload($foto['tmp_name'], $foto['name'], $nomeImg, '350', '../usuarios/');
    $now = date('Y-m-d H:i:s');

        $_SESSION['downs_email'] = $email;
        $_SESSION['downs_senha'] = $senha;

        $atualizar_dados = BD::conn()->prepare("UPDATE 'usuarios' SET 'nome' = ?, 'sobrenome' = ?, 'email' = ?, 'usuario' = ?, 'senha' = ?, 'descricao' = ?, 'foto' = ?
        WHERE 'id' = ?");
        $dados_atualizar = array($nome, $sobrenome, $email, $usuario, $senha, $descricao, $nomeImg, $usuarioLogado->id);
        if($atualizar_dados->execute($dados_atualizar)){
            echo '<script>alert("Dados atualizados com sucesso!");location.href="?pagina=configs"</script>';
        }else{
            echo '<script>alert("Ocorreu algum erro ao editar");location.href="?pagina=configs"</script>';
        }
    }
endif;

Follow the Upload Function:

function upload($tmp, $name, $nome, $larguraP, $pasta){

    $ext = end(explode('.', $name));
    if($ext=='jpg' || $ext == 'JPG' || $ext == 'jpeg' || $ext == 'JPEG'){
            $img = imagecreatefromjpeg($tmp);
    }elseif($ext == 'png'){
            $img = imagecreatefrompng($tmp);
    }elseif($ext == 'gif'){
            $img = imagecreatefromgif($tmp);
    }
    list($larg, $alt) = getimagesize($tmp);
    $x = $larg;
    $y = $alt;
    $largura = ($x>$larguraP) ? $larguraP : $x;
    $altura = ($largura*$y)/$x;

    if($altura>$larguraP){
            $altura = $larguraP;
            $largura = ($altura*$x)/$y;
    }
    $nova = imagecreatetruecolor($largura, $altura);
    imagecopyresampled($nova, $img, 0,0,0,0, $largura, $altura, $x, $y);

    imagejpeg($nova, $pasta.$nome);
    imagedestroy($img);
    imagedestroy($nova);
    return (file_exists($pasta.$nome)) ? true : false;
}
    
asked by anonymous 09.05.2017 / 17:00

2 answers

1

To save the image itself, in a field of type BLOB in the database you should use:

$conteudoImagem = file_get_contents($foto['tmp_name']);

and replace in the array $ image_name by $ contentImage:

$dados_atualizar = array($nome, $sobrenome, $email, $usuario, $senha, $descricao, $conteudoImagem, $usuarioLogado->id);
    
10.05.2017 / 16:44
1

Hello, you should save the image on the server and in the database you save only the path from where the image was saved.

For example:

You save the file avatar.jpg to /var/www/html/upload/images

In the database you should save '/var/www/html/upload/images/avatar.jpg' or just the name of the image to retrieve it at a later time by concatenating the path with the name.

    
09.05.2017 / 22:17