Upload Multiple File in PHP MySQL Database Example

Mar 07, 2022 . Admin



Hello Friends,

I am going to explain you upload multiple file in PHP MySQL Database example. You will learn how to multiple upload file in db using PHP. In side this article we will see how to multiple upload files in PHP with Database.

This article will give you simple example of PHP MySQL multiple upload file. We will use get simple PHP code for multiple upload file in Database.

You can use from how to multiple upload file in Database using PHP and MySQL. I will give you simple how to PHP multiple upload file to Database.

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>Upload Multiple 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">
</head>
<body>
<?php 
    if (isset($_FILES['file'])) {

        $allowedFileType = array('image/jpg','image/png','image/jpeg');
        
        $servername = "localhost";
        $username = "root";
        $password = "root";
        $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, $allowedFileType)){
                
                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>Upload Multiple File in PHP MySQL Database Example - 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