The exercise I'm doing in my internship involves an upload page, a table page that'll output the records of the database table and a database table. The problem is that after a huge effort to get all well connected I'm having some troubles because the index.php page should sent the information and is sending the the array and saving array instead of the file. Here is the PHP code of the index.php page:
<?php
if (!isset($_POST['Enviar'])): // This if will search if the $_POST variable already has anything
or not.
$allowed = array('txt'); //This array contains the file types which are allowed.
$uploadtime = time();
$uploadfile = $_FILES['file_to_upload'];
include 'db_connection.php';
$sql="select * from uploads";
$result = $conn->query($sql);
$target_dir = "uploads/"; //variable that will save the name of the folder or way where the file
will be saved.
$target_file = $target_dir . basename($_FILES['file_to_upload']['name']); //NOTICE undefined index:
file_to_upload... because it doesn't have any value safe there yet.
$goon = 1; //This variable is used to check if there is any error messages or if the program can go
on.
$ext = pathinfo( $_FILES["file_to_upload"]["name"], PATHINFO_EXTENSION); // This will use the OS to
get the file extension.
if(!in_array($ext,$allowed) ) {
echo 'Erro: Ficheiro não permitido. <br> Por favor, verifique se o formato do ficheiro é txt.';
$goon = 0;
}
// Check if $goon is set to 0 by an error
if ($goon == 0) {
echo " O ficheiro não fez o upload.";
// if everything is ok, we'll upload the fie
} else {
if (move_uploaded_file($_FILES["file_to_upload"]["tmp_name"], $target_file)) {
echo "O ficheiro ". basename( $_FILES["file_to_upload"]["name"]). " fez o upload com sucesso.";
// This will insert the new record on the specified table into the database.
$sql = "INSERT INTO uploads (name,file_name,file_extension,uploaded_time)
VALUES('".$_POST['file_name']."','$uploadfile','$ext','$uploadtime')"; //something is wrong
in here: Notice: Array to string conversion in C:\xampp\htdocs\16082019\index.php on line 66; 66 line is this one.
var_dump($_FILES['file_to_upload']);
$temp_name = $_FILES['file_to_upload']; //Notice: Undefined index: file_name in
C:\xampp\htdocs\16082019\index.php on line 68 only shows up when it doesn't have a value there yet.
if ($conn->query($sql) === TRUE) { // This test will warn the user if the record was created
with success or not.
echo "<br>Novo registo do ficheiro introduzido com sucesso. <br>"; //Only output this
message after the test to verify if the download was successful or not.
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
echo "<br>";
}
} else {
echo "Pedimos desculpa, mas ocorreu um erro enquanto se fazia o upload do seu ficheiro."; //
because it doesn't have anything there it'll show this error message.
}
}
endif;
$conn->close();
?>
and here is the code of the table page:
<?php
$n= null;
$row = null;
echo "<table border='1' colspan='3'>";
echo "<tr><th colspan='3'bgcolor='BFDEFF'>Ficheiros já na Base de Dados:</th></tr>";
echo"<tr><td>Nome</td><td>Ficheiro</td><td>Data</td></tr>";
include 'db_connection.php';
$sql="select * from uploads";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$time = $row['uploaded_time'];
$n = implode('|',$row);
echo "<tr>";
echo "<td>" . $row['name'] . "</td>"; //name of the file
echo "<td><a href='16092019/uploads/$n'>" . $row['file_name'] . "</a></td>"; // the file
var_dump($row['file_name']);
echo "<td>" . date('d-m-Y H-i-s', $time) . "</td>"; //date of the upload
echo "</tr>";
}
echo "</table>";
$conn->close();
?>
The field names in this 2nd piece of code are the names in the database. Could anyone tell me what's wrong for me to receive this error message in the index.php page: Notice: Array to string conversion in C:\xampp\htdocs\16082019\index.php on line 66