<?php
error_reporting(1);
$server = "localhost";
$user_name = "****";//database username
$password = "****";//database password
$database = "sample";//database name
$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")
{
if (is_uploaded_file($_FILES['csvfile']['tmp_name']))
{
echo "<h1>" . "File ". $_FILES['csvfile']['name'] ." uploaded successfully." . "</h1>";
}
$handle = fopen($_FILES['csvfile']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$import="INSERT INTO csv(firstname,email) VALUES('$data[0]','$data[1]')";
$result = mysqli_query($connection, $import) or die("Error in Selecting " . mysqli_error($connection));
}
fclose($handle);
echo "Import done";
}
else
{
echo " Check Extension. your extension is ." . $ext;
}
}?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Upload a CSV File:- </td><td><input type="file" value="Upload CSV Format" name="csvfile" required />
<input type="submit" value="Upload" name="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
NOTE:
Create a csv(firstname,email) table inside the sample database and copy the above code and run it.
Comments
Post a Comment