-1

I am trying to build a little upload script which uploads data as a blob to my database. I made a really simple form like that:

<form action="admin.php" method="post">
    <input type="file" name="datei">
     <br>
     <input type="submit" value="Upload!">
 </form>

And my PHP File as that:

if (isset($_FILES["datei"]) && isset($_FILES["datei"]["tmp_name"]) && is_uploaded_file($_FILES["datei"]["tmp_name"])) {
    try {
        $con = new MySQLi("localhost", "tom", "DBdb123#", "db");
        if ($con) {
            $name = basename($_FILES["datei"]["tmp_name"]);
            $datei = $_FILES["datei"];

            $sql = "INSERT INTO dateien (name, datei) VALUES (?, ?)";
            $kommando = $db->prepare($sql);
            $kommando->bind_param("sb", $name, $datei);

            try {
                $kommando->execute();
                echo "Upload erfolgreich!";
            } catch(Exception $ex) {
                echo "Fehler: " . $ex->getMessage() . "!";
            }
            $db->close();
        }
    } catch (Exception $ex) {
        echo "Fehler: " . $ex->getMessage();
    }
}

So I request a error message after both try's but after a test, nothing happened. No error, no successfull message, no database entry.. just as I didn't click/upload anything. I am trying this for an hour now and nothing changes.. that's really frustrating.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Tom
  • 47
  • 6
  • 2
    you need 'enctype="multipart/form-data"' in your FORM – Honk der Hase May 18 '20 at 10:22
  • What is the first `if` fails? You get nothing. So I'd look into that. – Bart Friederichs May 18 '20 at 10:24
  • 1
    ALso `$datei = $_FILES["datei"];` wil set `$datei` to an array, which WONT work with `$kommando->bind_param("sb", $name, $datei); ` – RiggsFolly May 18 '20 at 10:24
  • Also I assume as the file column is defined as a blob, you are trying to store the file onto the database and not just the path to it on the file system. This means you will need to read the file, so you can store it – RiggsFolly May 18 '20 at 10:30
  • @RiggsFolly Mhh alright, adding that form-data attribute to the form made the process work and display errors. You're right, I am using a blob column, I'll have to learn how to read a file etc. Thank you, sir! – Tom May 18 '20 at 10:42
  • try `$datei = file_get_content($_FILES["datei"]["tmp_name"]);` – RiggsFolly May 18 '20 at 10:43
  • @RiggsFolly Mhh it didn't drop any errors (after adding a "s" to content) but the blob field seems to keep empty in the "phpmyadmin"-view. – Tom May 18 '20 at 10:51
  • Have a read on this https://stackoverflow.com/questions/2188264/viewing-content-of-blob-in-phpmyadmin – RiggsFolly May 18 '20 at 11:29

1 Answers1

1

You should mention enctype="multipart/form-data in your code when uploading files.

Change your code to

<form action="admin.php" method="post" enctype="multipart/form-data">
    <input type="file" name="datei">
     <br>
     <input type="submit" value="Upload!">
 </form>
Kunal Raut
  • 2,495
  • 2
  • 9
  • 25