Save file names to DB and access them later?

0

I want that when uploading a pdf or ppt file on the site it becomes available so that the admins of the site can download. I am able to save the file to a folder, however I can not save it to the database, does anyone have any idea how to do this?

Here is the code for uploading and moving to the folder I chose to save:

<div>
<div style="padding-top: 300px;">
    <center><form method="post" enctype="multipart/form-data" action="">
      <input type="file" name="arquivo" />
      <input type="submit" class="btn btn-primary" value="enviar arquivo" name="enviar">
    </form></center>
</div>
</div>
<center><p class="text-center text-success" style="font-size: 20px; padding-bottom: 300px;">
<?php
  if (isset($_POST['enviar'])) {


    move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$_FILES['arquivo']['name']);
    echo "Arquivo enviado com sucesso!!";

  }

?>
</p></center>
    
asked by anonymous 02.10.2017 / 14:59

2 answers

1

To save to the bank, you need to register the file name, eg:

<div>
<div style="padding-top: 300px;">
    <center><form method="post" enctype="multipart/form-data" action="">
      <input type="file" name="arquivo" />
      <input type="submit" class="btn btn-primary" value="enviar arquivo" name="enviar">
    </form></center>
</div>
</div>
<center><p class="text-center text-success" style="font-size: 20px; padding-bottom: 300px;">
<?php
  if (isset($_POST['enviar'])) {


    move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$_FILES['arquivo']['name']);
    $sql = $pdo->prepare("INSERT INTO tabela SET arquivo = ?");
    if($sql->execute(array($_FILES['arquivo']['name']))){
        echo "Arquivo enviado com sucesso!!";
    }


  }

?>
</p></center>

Remembering that the variable $pdo is the connection, then you need to create a connection to the bank.

    
02.10.2017 / 15:03
0

I was able to solve, with the help of Rafael Augusto's answer, using the following code:

<?php

  session_start();

  require_once('conecta.php');

  if (isset($_POST['enviar'])) {


    move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$_FILES['arquivo']['name']);

    $arq = $_FILES['arquivo']['name'];
    $email = $_SESSION['email'];
    echo $arq;
    echo $email;
    $objDb = new db();
    $link = $objDb->conecta_mysql();
    $sql = "insert into arquivos (email_vol, nomearq) values ('$email', '$arq')";
    if (mysqli_query($link, $sql)){
      echo 'Quinto plano de aula enviado com sucesso!';
    } else {
      echo 'Erro ao enviar o arquivo!';
    }

  } else {
    echo "Nenhum arquivo selecionado!";
  }

?>
    
03.10.2017 / 14:35