Código PHP:
<?php
include "upload.php"; //coneccion
$deleterecords = "TRUNCATE TABLE upload"; //borra los registros de upload
mysql_query($deleterecords);
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
readfile($_FILES['filename']['tmp_name']);
}
//importa archivo a la db
$handle = fopen($_FILES['filename']['tmp_name'], "n");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$import="INSERT into upload(nombre, apellidos, edad, estado, empresa) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
print "Import done";
//formulario upload
}else {
print "Subir Archivo<br />\n";
print "<form enctype='multipart/form-data' action='index.php' method='post'>";
print "Nombre de Archivo:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>