Skip to content

Instantly share code, notes, and snippets.

@333crist

333crist/upload.php

Last active Apr 21, 2018
Embed
What would you like to do?
Import Data Dari CSV ke MySQL dengan PHP Native
<h2>Upload Biodata</h2>
<?php
ini_set('max_execution_time', 3000);
if (!$_POST) { ?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
} else {
$connect = new mysqli("localhost", "root", "", "tb_biodata");
if ($_FILES['csv']['size'] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if ($i > 0) {
$import = "INSERT into tb_biodata(
id,nama,usia,domisili,create_update)
values(
'',
'$data[0]',
'$data[1]',
'$data[2]',
now()
)";
$connect->query($import);
}
$i++;
}
fclose($handle);
print "Import done";
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
}
else
{
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.