Save File to Database with PHP

Asked

Viewed 175 times

1

I have a web application, where everything is running smoothly with exception of file upload.

the PHP responsible for storing the data in the database and saving the file in the folder is this

<?php
    session_start();
    include_once("connect.php");

    $fname = filter_input(INPUT_POST, 'fname', FILTER_SANITIZE_STRING);
    $lname = filter_input(INPUT_POST, 'lname', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_email);
    $file = $_FILE['file']['name'];


            $result_file = "INSERT INTO work (fname, lname,email, file) VALUES (:fname, :lname,:email, :file)";
            $insert_msg = $con->prepare($result_file);
            $insert_msg -> bindParam(':fname',$fname);
            $insert_msg -> bindParam(':lname',$lname);
            $insert_msg -> bindParam(':email',$email);
            $insert_msg -> bindParam(':file',$file);


            if($insert_msg->execut()){
                $last_id = $con->lasInsertId();
                $folder = 'files/' .$last_id '/';

                mkdir($folder,0755);

                move_uploaded_file($_FILES['file']['tmp_name']$folder.$file);

                $_SESSION['msg'] = "<p style='color:gren;'> File sent successfully</p>";
            header("Location: index.php");
            }else{
                    $_SESSION['msg'] = "<p style='color:red;'>Failed to send file</p>";
                    header("Location: index.php")
            }

But he’s not saving any information or saving the file.

  • In if($insert_msg->execut()), shouldn’t be if($insert_msg->execute()) ? No exception is appearing?

1 answer

0


There seem to be some problems in the code presented and this must be generating errors in your php.

This line looks like it’s missing a , (comma)

/// move_uploaded_file( $_FILES['file']['tmp_name']$folder.$file );
///                                                ^
    move_uploaded_file( $_FILES['file']['tmp_name'], $folder.$file );
    ///                                            ^

I don’t know if php will complain but it is worth just put everything in caps:

/// FILTER_SANITIZE_email
    ///             ^
    FILTER_SANITIZE_EMAIL
    ///             ^ 

The variable $_FILES this without the S:

/// $file = $_FILE['file']['name'];
    ///           ^
    $file = $_FILES['file']['name'];
    ///           ^

These I’m not sure, but I seem to be missing letters:

/// $insert_msg->execut()
    ///                ^
    $insert_msg->execute()
    ///                ^

/// $con->lasInsertId();
    ///      ^
    $con->lastInsertId();
    ///      ^ 

That one’s missing a . to concatenate:

 /// $folder = 'files/' .$last_id '/';
     ///                         ^
     $folder = 'files/' .$last_id. '/';
     ///                         ^

And the last header here missing a ; at the end of the line

/// header("Location: index.php")
    ///                          ^
    header("Location: index.php");
    ///                          ^

Reference: move_uploaded_file, $_FILES, FILTER_SANITIZE_EMAIL

Browser other questions tagged

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