<html>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<body>
<form method="post" enctype="multipart/form-data">
Upload a CSV File:- <input type="file" value="Upload CSV Format" name="csvfile" required />
<input type="submit" value="Upload" name="submit" />
</form>
</body>
</html>
<?php
error_reporting(1);
$server = "localhost";
$user_name = "root";
$password = "root";
$database = "sample";
$connection = mysqli_connect("$server","$user_name","$password","$database") or die("Error " . mysqli_error($connection));
$csvfile = $_FILES['csvfile']['name'];
$ext = pathinfo($csvfile, PATHINFO_EXTENSION);
$base_name = pathinfo($csvfile, PATHINFO_BASENAME);
if (isset($_POST['submit']))
{
if($ext == "csv")
{
$handle = fopen($_FILES['csvfile']['tmp_name'], "r");
echo "<html><body><table><tr><th>Name</th><th>Email</th></tr>";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
echo "<tr>";
foreach ($data as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>";
}
fclose($handle);
echo "</table></body></html>";
}
else
{
echo " Check Extension. your extension is ." . $ext;
}
}
?>
output:
After uploading a csv file,you can see the table as follows
Name | Email |
akhil | akhil@gmail.com |
mahesh | mahesh@gmail.com |
srinu | srinu@gmail.com |
Comments
Post a Comment