php is not writing csv

0

and tried to write a csv file with php, somehow you are not writing anything and I would like to know what it could be, if I recommend a php editor for linux, because I'm working with the nano editor from the console, the code is

<?php
date_default_timezone_set('UTC');
$fechaSolicitacion = date('Y-m-d H:i:s');
$nombreEvento = $_POST["nombreEvento"];
$Solicitante = $_POST["Solicitante"];
$emailSolicitante = $_POST["emailSolicitante"];
$servicio = $_POST["servicio"];
$fechaEvento =$_POST["fechaEvento"];
$horaFin = $_POST["horaFin"];
$horaInicio = $_POST["horaInicio"];
$organiza = $_POST["organiza"];
$nombreResponsable = $_POST["nombreResponsable"];
$cargo = $_POST["cargo"];
$telefono = $_POST["telefono"];
$celular = $_POST["celular"];
$responsableTecnico = $_POST["responsableTecnico"];
$emailTecnico = $_POST["emailTecnico"];
$iniciohora = (int) substr($horaInicio, 0, 2);
$finhora = (int) substr($horaFin, 0, 2);
$iniciominutos = (int) substr($horaInicio, 3, 4);
$finminutos = (int) substr($horaFin, 3, 4);
$minutos = $finminutos - $iniciominutos;
if ($minutos < 0){
$minutos = $minutos*(-1);
}
$duracion = ($finhora - $iniciohora)*60 + $minutos;
echo $duracion . '<br \>';
$iniciohora = $iniciohora + 5;
$hora = $fechaEvento . " " .  $iniciohora . ":" . $iniciominutos;
echo "hora: " . $hora. '<br \>';
$hora = strtotime($hora);
$hora = gmdate("Y M d H:i:s T", $hora);
$fechaSolicitacion = strtotime($fechaSolicitacion);
$fechaSolicitacion = gmdate("Y M d H:i:s T", $fechaSolicitacion);
$csv_line = array($nombreEvento, $Solicitante, $emailSolicitante, $servicio, 
$fechaEvento, $horazoom, $duracion, $organiza, $nombreResponsable, $cargo, 
$telefono, $celular, $responsableTecnico, $emailTecnico); 
$path = 'reuniones.csv';
$fop = fopen($path, 'w');
foreach ($csv_line as $line){
fputcsv($fop, $line);
}
rewind($fop);
fclose($fop);
?>

all files are and exist in / var / www / html the php is executed from an html, I know that I collect the data from the html form. the operating system is ubuntu.

    
asked by SERGIO AUGUSTO CARDONA RUEDA 01.11.2018 в 23:52
source

1 answer

0

I've tried your code and it works like I told you in comment.

Maybe in your case the file does not exist or that you do not have writing permission. You can control both situations by doing something like this:

$msg="";
$path = 'reuniones.csv';
if (file_exists($path)){
    if (is_writable($path)){
        $csv_line = array('a','b');
        $fop = fopen($path, 'w');
        fputcsv($fop, $csv_line);
        rewind($fop);
        fclose($fop);
        $msg="Archivo modificado correctamente".PHP_EOL;
    }else{
        $msg.="No se puede escribir en el archivo".PHP_EOL;
    }
}else{
    $msg.="El archivo no existe".PHP_EOL;
}
echo $msg;
    
answered by 02.11.2018 в 15:30