Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/04/2010, 16:59
Apokryphos
 
Fecha de Ingreso: noviembre-2009
Ubicación: Valencia
Mensajes: 7
Antigüedad: 14 años, 5 meses
Puntos: 0
Vaciar un archivo CSV a una tabla con php

Buenas noches o días según corresponda.

Mi problema:
Necesito que los usuarios carguen una informacion a una base de datos a través de un archivo CSV; pero que la información vaya directamente a un tabla. entonces se me ocurrió que la manera mas fácil era importar los archivos a un directorio y de allí crear un script que los cargue automáticamente. y es específicamente esa parte de mi plan el que no logro concebir.

en si la quiero leer del pc del usuario y cargarla a la tabla temporal de mi BD

Lo que he conceguido hasta ahora:
es que cargue el archivo a un directorio del servidor.

estos son mis archivos:


index.html
<?php
session_start();
include ('variables.php');


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<meta HTTP-EQUIV="Expires" CONTENT="Mon, 04 Dec 1999 21:29:02 GMT">
<meta name="Keywords" content="...">
<meta name="Description" content="Sinceracion de nominas 2010">
<title>qqqqqqqqqqqqqqqqqqqqqqqqqqq</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 20px;
margin-bottom: 25px;
}
.Estilo1 {font-size: 10px}
.Estilo2 {
color: #FF0000;
font-weight: bold;
}
-->
</style></head>

<body>
<div align="center">
<p>&nbsp;</p>
<form action="sincerar.php" method="post" enctype="multipart/form-data">
<table width="722" height="187" border="0">
<tr>
<td colspan="3"><h1 align="center">Sqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq </h1>
<h5 align="center">PA' QUE SEAN SERIOS </h5></td>
</tr>
<tr>
<td height="51" colspan="2"><span class="Estilo2">Seleccione el tipo de nomina:
<select name="tabla">
<option>becados</option>
<option>facilitadores_r1</option>
<option>facilitadores_r2</option>
<option>supervisores</option>
</select>
</span></td>
<td width="271"><label> <span class="Estilo2"><br />
Accion:</span><br />
<span class="Estilo1">
<input name="accion" type="radio" value="activos" />
ACTIVOS<br />
<input name="accion" type="radio" value="eliminar" />
ELIMINAR</span><br />
</label></td>
</tr>
<tr>
<td width="198">
<span class="Estilo2"><br />
Seleccione el archivo base:</span><br />
<input name="archivo" type="file" class="casilla" id="archivo" size="35" />
<input name="action" type="hidden" value="upload" />
</td>
<td width="231">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>

</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p><p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Sincerar" />
</p>
</form>

</div>
</body>
</html>




sincerar.php

<?php
$status = "";
if ($_POST["action"] == "upload") {
// obtenemos los datos del archivo
$tamano = $_FILES["archivo"]['size'];
$tipo = $_FILES["archivo"]['type'];
$archivo = $_FILES["archivo"]['name'];


if ($archivo != "") {
// guardamos el archivo a la carpeta files
$destino = "files/".$archivo;
if (copy($_FILES['archivo']['tmp_name'],$destino)) {
$status = "Archivo subido: <b>".$archivo."</b>";
} else {
$status = "Error al subir el archivo";
}
} else {
$status = "Error al subir archivo";
}
}

include ('data_conect.php');
include ('variables.php');

$arrResult = array();
$arrLines = file( 'files/facilitadores.csv' );
foreach( $arrLines as $line ) {
$arrResult[] = explode( ',', $line );
}

$sql = "SELECT tabla1.`NACIONALIDAD`,tabla1.`CEDULA`,tabla1.`PRIM ER APELLIDO`,tabla1.`SEGUNDO APELLIDO`,tabla1.`PRIMER NOMBRE`,tabla1.`SEGUNDO NOMBRE`,tabla1.`FECHA DE INGRESO`,tabla1.`FECHA DE ABONO`,tabla1.`NUMERO`,tabla1.`ESTADO`,tabla1.`BAN CO`,tabla1.`MUNICIPIO` FROM ".$tabla." AS tabla1,temporal AS tabla2 WHERE tabla1.CEDULA=tabla2.CEDULA";

$bus = mysql_query($sql) or die(mysql_error());

while ($row=mysql_fetch_array($bus))
{
$sql3= "INSERT INTO ".$tabla.""._."".$accion." (`NACIONALIDAD`,`CEDULA`,`PRIMER APELLIDO`,`SEGUNDO APELLIDO`,`PRIMER NOMBRE`,`SEGUNDO NOMBRE`,`FECHA DE INGRESO`,`FECHA DE ABONO`,`NUMERO`,`ESTADO`,`BANCO`,`MUNICIPIO`) VALUES ('".$row['NACIONALIDAD']."', '".$row['CEDULA']."', '".$row['PRIMER APELLIDO']."', '".$row['SEGUNDO APELLIDO']."', '".$row['PRIMER NOMBRE']."', '".$row['SEGUNDO NOMBRE']."', '".$row['FECHA DE INGRESO']."', '".$row['FECHA DE ABONO']."','".$row['NUMERO']."', '".$row['ESTADO']."', '".$row['BANCO']."', '".$row['MUNICIPIO']."')";

$bus3 = mysql_query($sql3) or die(mysql_error());
}
header("location: index.html");
?>