Difficulty sending Image to Database

Asked

Viewed 61 times

0

I’m having trouble sending an image to my database.

Only file name is going but File is not being Uploaded to BD.

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;
}
  • With what type of data you are storing the attachment in the Database?

2 answers

1


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

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

and replace in the $filename array with $countryImage:

$dados_atualizar = array($nome, $sobrenome, $email, $usuario, $senha, $descricao, $conteudoImagem, $usuarioLogado->id);
  • @Gustavojanctsch...thank you very much my fellow programmer saved me my brother. I’m amateur and I’m still learning vlw by sharing your knowledge

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 in /var/www/html/upload/images

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

  • my friend I’ve tried everything a little bit until I came to the conclusion that the best way would be to use Insert instead of Upload. But it has always happened the same thing my friend. It is just passing to the BD the File string. I use Easyphp-Devserver-14.1VC9.... If you can help me, man...

Browser other questions tagged

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