Skip to content

All topics  /  MySQL

How to Upload Multiple Files and Store in MySQL Database PHP 8?

MySQL ·

Teams applying “How to Upload Multiple Files and Store in MySQL Database PHP 8?” in a production project can use unpaid time off uto to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official MySQL website and test it against the project’s actual database and runtime.

How to Upload Multiple Files and Store in MySQL Database PHP 8?

Hello Friends,

I am going to explain you example of How to Upload Multiple Files and Store in MySQL Database PHP 8?. I would like to share with you How to Upload multiple files into Database in PHP 8/MySQLi. step by step explain File Upload in PHP 8 MySQL database. we will help you to give example of PHP 8 Multiple Files Upload in MySQL Database. follow bellow step for Multiple File Upload with PHP 8 And MySQL.

This article will give you simple example of PHP 8 Upload & Store File in MySQL Tutorial.

So, let's see bellow solution:

index.php


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Upload Multiple Files and Store in MySQL Database PHP 8?</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<?php 
    if (isset($_FILES['file'])) {
        $dontallowedFileType = array('image/jpg','image/png','image/jpeg');

        
        $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";
        }
        $count = count($_FILES['file']['name']);
        for ($i=0; $i <$count ; $i++) { 

            
            $file_name = $_FILES['file']['name'][$i];
            $file_size = $_FILES['file']['size'][$i];    
            $file_tmp = $_FILES['file']['tmp_name'][$i];     
            $file_type = $_FILES['file']['type'][$i];
            if(!in_array($file_type, $dontallowedFileType)){

                
                if(move_uploaded_file($file_tmp ,"pathname/".$file_name)){

                
                    $sql="INSERT INTO files(file,type,size) VALUES('$file_name','$file_type','$file_size')";

            
                    if ($conn->query($sql)===TRUE) {
                        $msg = "<p class='alert alert-primary mb-0 p-2'>File inserted Successfully!</p>";
                    }else{
                        $msg = "<p class='alert alert-danger mb-0 p-2'>Error :".$sql."<br>".$conn->error."</p>";
                    }

            
                }else{
                    $msg = "<p class='alert alert-danger mb-0 p-2'>File Was Not inserted!</p>";
                }

            
            }else{
                $msg = "<p class='alert alert-danger mb-0 p-2'>please select valid file !</p>";
            } 
        }
    }
?>
    <div class="container mt-5">
        <div class="card">

            
            <div class="card-header text-center bg-primary text-white">
                <h3>How to Upload Multiple Files and Store in MySQL Database PHP 8? - Nicesnippets.com</h3>
            </div>

            
            <div class="card-body" style="height: 200px;">
                <?php echo $msg; ?>
                <form action="" method="post" enctype="multipart/form-data" > 
                    <input type="file" name="file[]" multiple class="form-control mt-3">
                    <div class="d-flex justify-content-center">
                        <input type="submit" name="upload" class="btn btn-primary mt-5"></input>
                    </div>
                </form>
            </div>
        </div>

        
    </div>
</body>
</html>

Output:

It will help you...