how to create a txt file from a php form

1

good I have a doubt I'm new to php and I need to create a php form and have the data store it in a text document (.txt) this is the code that I have:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Configuracion Red</title>
    </head>
    <body>
        <form method="POST" ACTION="registro.php" >
            <table class="formulario">
                <tr>
                    <td class="label">
                        Numero ip :
                    </td>
                    <td class="entrada">
                        <input type "text" id="txtnumeroip" name="txtnumeroip" value="" maxlength = "15"  />
                    </td>
                </tr>
                <tr>
                    <td class="label">
                        Nombre Red Wifi :
                    </td>
                    <td class="entrada">
                        <input type="text" id="txtnombrewifi" name="txtnombrewifi" value="" maxlength="30"  />
                    </td>
                </tr>
                <tr>
                    <td class="label">
                        Clave :
                    </td>
                    <td class="entrada">    
                        <input type="text" id="txtClave" name="txtClave" value="" maxlength="20"  />
                    </td>   
                </tr>
                <tr>
                    <td colspan="2" class="botonera">
                        <input class="boton" type="submit" name="submit" value="Enviar">
                    </td>
                </tr>
            </table>    
<?php
    $archivo = "datos.txt";
    $gestor = @fopen("/tmp/datos.txt", "w+");

    fclose($gestor);
?>  
        </form>

    </body>
</html>

I receive the data here

<html>
<head>
<meta charset="UTF-8">
<?php

    $numeroIp="";
    if (!empty($_POST["txtnumeroip"]))
    {
        $numeroIp = $_POST["txtnumeroip"];
    }

    $nombrewifi="";
    if (!empty($_POST["txtnombrewifi"]))
    {
        $nombrewifi = $_POST["txtnombrewifi"];
    }

    $clave="";
    if(!empty($_POST["txtClave"]))
    {
        $clave = $_POST["txtClave"];
    }

    $contenido="
numero ip : $numeroip
nombre red wifi : $nombrewifi
clave : $clave
";

    $archivo = fopen("tmp/datos.txt","w+");
    fwrite($archivo,$contenido);
?>
</head>
<body>
    <h1>recibido</h1>
</body>
</html>

however, I do not create the file in the tmp folder

If someone can guide me in what I am doing wrong or what is the error would be appreciated

    
asked by Adolfo Comas 27.09.2017 в 20:04
source

1 answer

1

You can store the data in this way

if (file_exists("tmp/datos.txt")){
$archivo = fopen("tmp/datos.txt", "a");
fwrite($archivo, PHP_EOL ."$contenido");
fclose($archivo);
}
else{
$archivo = fopen("tmp/datos.txt", "w");
fwrite($archivo, PHP_EOL ."$contenido");
fclose($archivo);
   }
  

PHP_EOL

create a line break

    
answered by 28.09.2017 / 04:11
source