0
Hello, I have a code, explained below, where I want to make available the files that users uploaded for download, for this I made a php code, but the query is only executed if I upload a file, but I wanted it to run when loading the page, does anyone know any way to make the query run when starting the page or if there is something wrong with my code? Thanks in advance!
First I made the connection to the database, as shown below:
<?php
class db {
//host
private $host = 'localhost';
//usuario
private $usuario = 'iddsafdsfr';
//senha
private $senha = 'fasdfasd';
//banco de dados
private $database = 'fasdfasdg';
public function conecta_mysql(){
//criar a conexão
$con = mysqli_connect($this->host, $this->usuario, $this->senha, $this->database);
//ajustar a charser de cominicação entre a aplicação e o bd
mysqli_set_charset($con, 'utf8');
//verificar se houve erro de conexão
if (mysqli_connect_errno()) {
echo 'Erro ao tentar se conectar com o banco de dados'.mysqli_connect_error();
}
return $con;
}
}
?>
With the database connection made, I created the table directly in phpmyadmin and did the form part, where the user would insert the files, as follows in the code below:
<div class="container">
<div class="text-center">
<center><h1>Plano de aula 1</h1><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-success" style="font-size: 20px; padding-bottom: 100px;">
<?php
session_start();
require_once('conecta.php');
$email = $_SESSION['email'];
if (isset($_POST['enviar'])) {
$arq = $_FILES['arquivo']['name'];
$dataup = date('Y-m-d H:i:s');
echo $dataup;
$arq = str_replace(" ", "_", $arq);
$arq = str_replace("ç", "c", $arq);
if (file_exists("uploads/".$arq)) {
$a = 1;
while (file_exists("uploads/[".$a."]".$arq)) {
$a++;
}
$arq = "[".$a."]".$arq;
}
if (move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$arq)) {
$objDb = new db();
$link = $objDb->conecta_mysql();
$sql = "insert into arquivos (email_vol, nomearq) values ('". $email."', '".$arq."')";
if (mysqli_query($link, $sql)){
echo 'Plano de aula 1 enviado com sucesso!';
} else {
echo (mysqli_error($link));
echo 'Erro ao enviar o plano de aula!';
}
} else {
echo "Nenhum arquivo selecionado!";
}
} else{
}
?>
</p></center>
And finally I did the part where it was to show all the files that were uploaded according to the email and make available for download. But as I said before, the query $consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
is only executed if a new upload is made, but I would like it to be executed when loading the page, so that the user sees which files he has already uploaded.
<?php
session_start();
$email = $_SESSION['email'];
echo $email;
require_once('conecta.php');
$pasta = "uploads/";
$consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
var_dump($consulta);
while ($resultado = mysqli_fetch_array($consulta)) {
echo "<a href=\"" . $pasta . $resultado["nomearq"] . "\">" . $resultado["nomearq"] . "</a><br />";
}
?>
Arthur with this code only can not know how it works well, try to give a read on this https://answall.com/help/no-one-answers and edit the question - I am grateful.
– Guilherme Nascimento
@Guilhermenascimento I tried to do my best to make the question more complete.
– Arthur Oliveira
I didn’t understand the code very well... but try to create a function that executes the mysqli query, then put to execute the function at the beginning of the page, and again when the input receives the Submit
– Matheus Lopes Marques
The session_start() function should appear before any <html tag>:
– user60252
To start a Session, we use the session_start() function. For proper functioning, it cannot be after any data OUTPUT (echo, print, HTML codes, etc.). It is recommended to be in the first line of the code.
– user60252
@Arthuroliveira is the last part that will display the data on the same form page or is in another file?
– WMomesso
@Wmomesso is on the same form page, will that be it?
– Arthur Oliveira