How to upload txt file on form submit

Project : in this project you will learn how to upload txt file using form submit.

Data will be inserted in database table.
index.php
              
                <?php include("upload-txt.php");  ?>
                  <!DOCTYPE html>
                  <html>
                  <head>
                    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
                  </head>
                  <body>
                    <div class="container "><br>
                        <div class="row mt-2">
                            <div class="col-md-6 offset-3 border p-2" style="background: #f5f6fa;"> 
                              <div class="w-100 text-center"><?php echo $error; ?><?php echo $msg; ?></div>    
                              <form method="POST" action="#" enctype="multipart/form-data"><br>
                                  <h3 class="rounded bg-primary w-100 p-2 text-white text-center">Upload TXT File</h3>  
                                <div class="form-group">
                                  <label for="file"><b>Upload CSV File :</b></label>
                                  <input type="file" name="txtfile" id="txtfile" class="form-control" style="height: 44PX;">
                                </div><br>
                                <div class="form-group">
                                <input type="submit" name="txt" class="btn btn-success" value="Upload TXT File">
                                </div>
                            </form>
                            </div>
                          </div>       
                    </div>
                  </body>
                  </html>
              
            
CSV File
user_data.txt
PHP
Upload Form
Select user_data.txt from project folder or from anywhere, it should be txt file to upload
PHP

Database and table name

You have to create database and table in phpMyAdmin

check image

PHP

Create table in database

Table in database

                
                  CREATE TABLE `datas` (
                    `id` int(11) NOT NULL  PRIMARY KEY AUTO_INCREMENT,
                    `fullname` varchar(100) NOT NULL,
                    `mobile` varchar(20) NOT NULL,
                    `email` varchar(100) NOT NULL,
                    `city` varchar(100) NOT NULL,
                    `country` varchar(100) NOT NULL,
                  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                
            

Create database connection

Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Insert data & Save as txt File

Process to upload txt file and insert data into database table from txt file.

upload-txt.php
                
                <?php  
                  include('dbcon.php');
                  msg=''; $error='';
                  if(isset($_POST['txt'])){
                    $fileName = $_FILES['txtfile']['name'];
                    $fileTmpName = $_FILES['txtfile']['tmp_name'];
                    //find path the extension of file
                    $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
                      if(empty($fileName)){
                        $error = "<div class='alert alert-info'><span style='color:red;'>The field is required !!</span></div>"; 
                      }else{
                          $allowedType = ['txt'];
                          if(!in_array($fileExtension, $allowedType)) {
                              $error = "<div class='alert alert-info'><span style='color:red;'>Invalid file Extension !!</span></div>";
                          }else{
                              if(($handle = fopen($fileTmpName, "r")) !== FALSE){
                                while($mydata = fgetcsv($handle,1000,',')){
                                    $fullname = $mydata[0];
                                    $mobile   = $mydata[1];
                                    $email    = $mydata[2];
                                    $city     = $mydata[3];
                                    $country  = $mydata[4];
                                    $query = "INSERT INTO `datas` (`fullname`,`mobile`,`email`,`city`,`country`) VALUES('$fullname','$mobile','$email','$city','$country')";
                                    $run = mysqli_query($dbcon,$query);
                                    if($run){
                                      $msg = "<div class='alert alert-success'><span style='color:green;'>The excel file has been uploaded into database !!</span></div>";
                                    $fullname = ''; $mobile = ''; $email = ''; $city = ''; $country = '';
                                    }else{
                                      $error = "<div class='alert alert-info'><span style='color:red;'>Something went wrong in uploading file  !!</span></div>";
                                    } 
                                }
                              fclose($handle); 
                            }
                          }
                        }
                     }
                  
              
Database table after uploaded txt file

Check database table after uploading file


TXT File Format

myproject/user-data.txt
user-data.txt
                
                  Demo Singh, 43424343, demO@demo.com, Ludianan, India, 
                  Dill Mange More, 10101010101, more@demo.com, Lucknow, India, 
                
            

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css