PHP 8 Upload Multiple Files and Store in MySQL Database

Jun 03, 2022 . Admin



Hello Friends,

Here, I will show you how to works PHP 8 Upload Multiple Files and Store in MySQL Database. We will look at example of How to upload multiple files and store in the MySQL database using PHP 8. I explained simply step by step Upload Multiple Files to Store in MySQL Database Using PHP 8. I explained simply about How to Upload multiple files into Database in PHP 8/MySQLi. Follow bellow tutorial step of PHP 8 Multiple Files Upload in MySQL Database.

This article will give you simple example of Multiple File Upload with PHP 8 And MySQL.

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>PHP 8 Upload Multiple Files and Store in MySQL Database</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/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>PHP 8 Upload Multiple Files and Store in MySQL Database - Mywebtuts.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...

#PHP 8