Save filename in BD and access them later?

Asked

Viewed 128 times

0

I want it to be available when uploading a pdf or ppt file on the site so that the admins of the site can download. I’m managing to save the file in a folder, but I can’t save in the database, does anyone have any idea how to do this?

Here is the code to upload and switch 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>

2 answers

1


To save to the bank, Voce needs to register the file name, Ex:

<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, so you need to create a connection to the bank.

  • @Stormwind As far as I know, both syntaxes are equivalent. one is the SQL standard, the other is the Mysql extension.

  • https://dev.mysql.com/doc/refman/5.6/en/insert.html

0

I managed to solve, with the help of Rafael Augusto’s reply, 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!";
  }

?>

Browser other questions tagged

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