Upload File in PHP MySQL Database Example

Mar 03, 2022 . Admin

Hello Friends,

I am going to explain you PHP MySQL file upload example. You will learn how to upload file in PHP MySQL database example. In side this article we will see how to Upload and Store Image and File in Database using PHP and MySQL.

This article will give you simple example of File/Image Upload in PHP MySQL Database. We will use get simple PHP code for upload File in Database.

You can use from how to upload File in Database using PHP and MySQL. I will give you simple how to PHP upload any type File to Database.

So, let's see bellow solution:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Upload File in PHP MySQL Database Example</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
if (isset($_FILES['file'])) {
$file_name = $_FILES['file']['name'];    
$file_size = $_FILES['file']['size'];    
$file_tmp = $_FILES['file']['tmp_name'];     
$file_type = $_FILES['file']['type'];        

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_php";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("connection failed:" .mysqli_connect_error());
    echo "connection successfull";

    $sql="INSERT INTO files(file,type,size) VALUES('$file_name','$file_type','$file_size')";
    if ($conn->query($sql)===TRUE) {
        echo " ";
        $msg = "<h5 class='alert alert-primary'>Error :".$sql."<br>".$conn->error."</h5>";
    if (move_uploaded_file($file_tmp ,"pathname/".$file_name)) {
        $msg = "<h5 class='alert alert-primary'>File inserted Successfully!</h5>";
        $msg = "<h5 class='alert alert-primary'>File Was Not inserted!</h5>";
    <div class="container mt-5">
        <div class="card">
            <div class="card-header text-center bg-primary text-white">
                <h3>Upload File in PHP MySQL Database Example -Mywebtuts.com</h3>
            <div class="card-body" style="height: 200px;">
                <?php echo $msg; ?>
                <form action="" method="post" enctype="multipart/form-data" > 
                    <input type="file" name="file" class="form-control" class="custom-input-file">
            <div class="d-flex justify-content-center pb-5">
                    <input type="submit" name="upload" class="btn btn-primary"></input>


It will help you...