Upload files and images with PHP and Mysql

Asked

Viewed 434 times

1

I’ve got a bug in my code only I don’t understand

that is the code:

<?php

    $db = new PDO("mysql:host=localhost;dbname=filedb;charset=utf8", "root", "");

    $msg = false;

    if(isset($_FILES['arquivo'])){

        //nome do arquivo
        $nome = $_FILES['arquivo']['name'];

        //renomear
        $point = substr($nome , -4 , 1);
        if($point == '.'){
            $extensao = strtolower(substr($nome, -4));
        }else{
            $extensao = strtolower(substr($nome, -5));
        }
        //novo
        $novo_nome = md5(time()). $extensao;
        // local de armazenamento permanece
        $local = 'upload/';

        if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $local.$novo_nome)){
            $img = $db->prepare("INSERT INTO arquivo ( img ) VALUES ( :imagem) ");
            $img->execute(
                array(
                    ':imagem'=>$novo_nome
                )
                );
        }else{
            $msg = "erro não foi possivel enviar este arquivo";
        }
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>FILE</title>
</head>
<body>
    <h1>Arquivo</h1>
<?php if($msg != false) echo "<p> $msg </p>"; ?>
    <hr>
    <br>
    <form action="index.php" method="POST" enctype="multipart/form-data">
        Arquivo : <input type="file" name="arquivo" required>
        <input type="submit" value="Salvar">
    </form>
</body>
</html>

is that a Brigadão and good morning to all...

  • Explain better what your problem is.

No answers

Browser other questions tagged

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