Upload multiple photos using image path (PHP and Mysql)

Asked

Viewed 28 times

-2

Well, I’m having a hard time taking the photo that the user chooses on the computer and playing in the database and then throwing it in a folder created in my project so that I can display the image, I’ve already broken my head a lot, I’m not an expert in php, and I need help in this part to complete my project.

Here is my html form with the form! Remembering that I can insert the text data into my database table. Only the images are missing to be complete.

<form action="../routes/upload_anuncio.php" method="POST" enctype="multipart/form-data">
                <?php if (isset($_SESSION['cad_sucesso'])): ?>
                    <div class="alert alert-success" role="alert">
                        <?=$_SESSION['cad_sucesso'];?>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                <?php 
                endif; 
                unset($_SESSION['cad_sucesso']);
                ?>

                <?php if (isset($_SESSION['cad_err'])): ?>
                    <div class="alert alert-danger" role="alert">
                        <?=$_SESSION['cad_err'];?>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                <?php 
                endif; 
                unset($_SESSION['cad_err']);
                ?>
                    
                    <div class="row">
                        <div class="col-sm">
                            <label>Funcionário:</label>
                            <input type="text" class="form-control" name="post_funcionario" placeholder="Nome do Funcionário" value="<?php echo $_SESSION['nome_funcionario']; ?>" required>
                        </div>
                        <div class="col-sm">
                            <label>Tipo de Anuncio:</label>
                            <select class="form-control" name="tipo_anuncio" required>
                                <option value="Aluguel" selected>Aluguel</option>
                                <option value="Venda">Venda</option>
                            </select>
                        </div>
                        <div class="col-sm">
                            <label>Cidade:</label>
                            <select class="form-control" name="cidade" required>
                                <option value="Juazeiro do Norte - CE" selected>Juazeiro do Norte - CE</option>
                                <option value="Crato - CE">Crato - CE</option>
                                <option value="Barbalha - CE">Barbalha - CE</option>
                            </select>
                        </div>
                        <div class="col-sm">
                            <label>CEP</label>
                            <input type="text" class="form-control" id="cep" name="cep" placeholder="CEP" required>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-sm">
                            <label>Endereço:</label>
                            <input type="text" class="form-control" name="endereco" placeholder="Endereço" required>
                        </div>
                        <div class="col-sm">
                            <label>N°:</label>
                            <input type="text" class="form-control" name="num_casa" placeholder="123, 123 A" required>
                        </div>
                        <div class="col-sm">
                            <label>Bairro:</label>
                            <input type="text" class="form-control" name="bairro" placeholder="Bairro" required>
                        </div>
                        <div class="col-sm">
                            <label>Visibilidade:</label>
                            <select class="form-control disabled" name="visibilidade">
                                <option value="Disponível" selected>Disponível</option>
                                <option value="Indisponível">Indisponível</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-sm">
                            <label>Telefone / Celular:</label>
                            <input type="text" class="form-control" id="telefone" name="telefone" placeholder="Telefone / Celular" required>
                        </div>
                        <div class="col-sm">
                            <label>Whatsapp:</label>
                            <input type="text" class="form-control" id="wpp" name="wpp" placeholder="Whatsapp" required>
                        </div>
                    </div>

                    <br>

                    <div class="row">
                        <div class="col-sm">
                            <label>Foto da Fachada:</label>
                            <input type="file" name="ftfachada" class="form-control-file" multiple required>
                        </div>
                        <!--<div class="col-sm">
                            <label>Foto dos Cômodos:</label>
                            <input type="file" name="ftcomodos[]" class="form-control-file" multiple required>
                        </div>-->
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-sm">
                            <label>Valor:</label>
                            <input type="text" class="form-control" id="valor" name="valor" placeholder="R$ 0.000,00" required>
                        </div>
                        <div class="col-sm">
                            <label>Este valor pode ser negociável?</label>
                            <select class="form-control" name="valor_neg" required>
                                <option value="Sim">Sim</option>
                                <option value="Não">Não</option>
                            </select>
                        </div>
                        <div class="col-sm">
                            <label>Quantidade de Cômodos:</label>
                            <select class="form-control" name="qtd_comodos" required>
                                <option value="3" selected>3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="Mais de 10">Mais de 10</option>
                            </select>
                        </div>
                    </div>
                    <br>

                    <div class="row">
                        <div class="col-xl-3 col-md-6">
                            <button type="submit" name="salvar_anuncio" class="w-100 btn btn-anunciar btn-lg">Anunciar</button>
                        </div>
                    </div>

                </form>

Here is the code that processes all information

<?php 
    session_start();
    include "../config/conn.php";

    

        $post_funcionario   = $_POST['post_funcionario'];
        $tipo_anuncio       = $_POST['tipo_anuncio'];
        $cidade             = $_POST['cidade'];
        $cep                = $_POST['cep'];
        $endereco           = $_POST['endereco'];
        $num_casa           = $_POST['num_casa'];
        $bairro             = $_POST['bairro'];
        $visibilidade       = $_POST['visibilidade'];
        $telefone           = $_POST['telefone'];
        $wpp                = $_POST['wpp'];
        $ftfachada          = $_FILES['ftfachada']['name'];
        $valor              = $_POST['valor'];
        $valor_neg          = $_POST['valor_neg'];
        $qtd_comodos        = $_POST['qtd_comodos'];

            //pegar id do usuario cadastrado
            $id_user_anun = $_SESSION['id'];
            
            $query = "INSERT INTO criar_anuncio (nome_funcionario, tipo_anuncio, cidade, cep, endereco, numero_casa, bairro, visibilidade, telefone, wpp, foto_fachada, valor, valor_neg, qtd_comodos, id_user_anun, data_cadastro) VALUES ('$post_funcionario', '$tipo_anuncio', '$cidade', '$cep', '$endereco', '$num_casa', '$bairro', '$visibilidade', '$telefone', '$wpp', '$ftfachada', '$valor', '$valor_neg', '$qtd_comodos', '$id_user_anun', NOW())";
            $new_query = mysqli_query($conn, $query);
            
            if (mysqli_affected_rows($conn) != 0) {
                $_SESSION['cad_sucesso'] = 'Anúncio Feito! Acesse o <b>Painel</b> ou o seu <b>Perfil</b> para visualizá-lo.';
                header("Location: ../pages/criar_anuncio.php");

            }else{
                $_SESSION['cad_err'] = 'Não foi possivel realizar o Anúncio! Tente novamente.';
                header("Location: ../pages/criar_anuncio.php");

            }

    $conn->close();
    exit;
?> 

I really don’t know how to proceed, and I’ll be waiting if anyone can help!

  • https://answall.com/search?q=Php+upload+de+imagens

1 answer

0


This issue of uploading files there are several examples over the internet, whenever possible try to do a search before, if you find nothing, ask your question.

But, let’s get to your point:

Let’s consider the following HTML snippet:

<form method='post' action='' enctype='multipart/form-data'>
   <input type="file" name="file[]" id="file" multiple>
   <input type='submit' name='submit' value='Upload'>
</form>

According to the above markup, we have prepared the form to receive multiple files.

<?php
// Verificando se recebemos alguma informação através do POST submit 
if (isset($_POST['submit'])) {
    // verificando a quantidade de arquivos enviado pelo formulário
    $countfiles = count($_FILES['file']['name']);
    // Tendo a quantidade de arquivos, é percorrido cada elemento
    for ($i=0;$i<$countfiles;$i++) {
        $filename = $_FILES['file']['name'][$i];
        // Para cada imagem encontrada, ela é movida ao destino escolhido, no caso foi definido o diretório upload
        move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);
    }
} 
?>

The reference of the code described is available on the website https://makitweb.com/multiple-files-upload-at-once-with-php/#:~:text=In%20PHP%2C%20it%20is%20possible,element%20to%20select%20multiple%20files.

Too many questions, usually you will never save the image in the database, but rather its path to the directory.

Browser other questions tagged

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