-1

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

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Marta
  • 23
  • 1
  • 10
  • Somewhere in your code (line 68) you are trying to use an array as string. Could be in some `echo` – Cid Sep 23 '19 at 09:48
  • The most of the echo are in another page (the 2nd piece of code) but thanks xD – Marta Sep 23 '19 at 10:23
  • @Dharman could you give an example please? – Marta Sep 23 '19 at 10:43
  • Sure. Please read the following article https://stackoverflow.com/a/60496/1839439 – Dharman Sep 23 '19 at 10:45
  • @Dharma, I understand that it's a line of defense that it's very useful for a website database, thank you for your suggestion but it won't solve the problem I have now in hands, but I'll save it for a future reference when I'll need to put it online xD – Marta Sep 23 '19 at 11:01

2 Answers2

0

1st: Thank you for trying to help me xD.

2nd: I already got a solution that works in the context of my exercise:

$uploadfile = $_FILES['file_to_upload']; 
$uploadfile2 = $uploadfile['name']; 

then substitute the old line for this:

VALUES('".$_POST['file_name']."','$uploadfile2','$ext','$uploadtime')";
Marta
  • 23
  • 1
  • 10
-1

"Notice: Array to string conversion" indicates you are trying to use an array as a string. This is because you are concatenating the array $_POST['file_name'] with your INPUT query string. You can convert an array to string using the implode() function:

$values = implode(',', $_POST['file_name']);
$sql = "INSERT INTO uploads (name,file_name,file_extension,uploaded_time)
        VALUES('" . $values . "','$uploadfile','$ext','$uploadtime')";
R. Silver
  • 39
  • 3
  • so instead of the $uploadfile variable I would use implode($uploadfile)? – Marta Sep 23 '19 at 10:14
  • `implode(',', $uploadfile)`. The implode function takes two arguments. The first is the separator string which has to be a comma (,) for your example above. For example: `$a = [1, 2, 3]; echo implode(',' $a);` Will print: `"1,2,3"` – R. Silver Sep 23 '19 at 10:26
  • It doesn't work because it that case the variable $values will substitute the name and it won't show up in the table, that field will be blank. and the problem is the field where is the variable $uploadfile. The file name should appear and serve as a hyperlink to the document download in the other page but it only shows the Array word. – Marta Sep 23 '19 at 10:39