<?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>
You have to create database and table in phpMyAdmin
check image
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;
$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);
Process to upload txt file and insert data into database table from txt file.
<?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);
}
}
}
}
Check database table after uploading file
Demo Singh, 43424343, demO@demo.com, Ludianan, India,
Dill Mange More, 10101010101, more@demo.com, Lucknow, India,