Created
April 16, 2019 20:28
-
-
Save gomezgleonardob/6bad46816951f6fc048128beda80ceaf to your computer and use it in GitHub Desktop.
CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$path = "./datos/abril.csv"; | |
if (unlink($path)) | |
{ | |
echo("Borrado"); | |
} | |
else | |
{ | |
$servername = "localhost"; | |
$username = "root"; | |
$password = ""; | |
$dbname = "agua"; | |
$conn = mysqli_connect($servername, $username, $password, $dbname); | |
if (!$conn) { | |
die("Connection failed: " . mysqli_connect_error()); | |
} | |
$sql = "select id,ph1,ox1,con1,ph2,ox2,con2 from datos where fecha >= '2019-04-09' and fecha <='2019-04-30' group by ((120/120) * HOUR( hora *0.5) + FLOOR( MINUTE( hora) / 120)) "; //hago la consulta a la base de datos | |
$query = $conn->query($sql) or die($conn->error); | |
$info = array(); //los datos se guardaran en este arreglo | |
while($result = $query->fetch_assoc()) | |
{ | |
$info[] = $result; //guardo cada resultado en este arreglo | |
} | |
$file = "abril.csv"; //le doy un nombre al archivo | |
file_put_contents($file, "x;Ph1;Oxig1;Cond1;Ph2;Oxig2;Cond2" . PHP_EOL); //creamos el archivo | |
for($i = 0; $i < count($info); $i++) | |
{ | |
file_put_contents($file, implode(";", $info[$i]), FILE_APPEND); //escribo en el archivo separando el arreglo con comas | |
file_put_contents($file, PHP_EOL, FILE_APPEND); //agrego un salto de linea | |
} | |
if (file_exists($file)) { //verifico que el archivo haya sido creado | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename='.basename($file)); | |
header('Content-Transfer-Encoding: binary'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . filesize($file)); | |
ob_clean(); | |
flush(); | |
readfile($file); | |
} else | |
{ | |
//en caso no se haya creado el archivo, muestro un mensaje | |
echo "Hubo un error al momento de crear el archivo, verifique los permisos de las carpetas del servidor."; | |
} | |
} | |
// Muestra la exepcion | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment