Doubt in php when generating txt files

0

I am trying to generate a simple .txt file with php and it does not allow it. This is my form:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Guardar archivos</title>
</head>
<body>
    <form action="guardar.php" method="POST" name="frm">
        <input type="text" name="nombre"><br><br>
        <textarea name="comentario"></textarea><br><br>
        <input type="submit" value="Guardar datos">
    </form>
</body>
</html>

and this is my file generator:

<?php 


$fi = fopen("archivo.txt", "a") or die ("Problemas al crear archivo");

$nom = $_REQUEST['nombre'];
$com = $_REQUEST['comentario'];

fwrite($fi, "Datos: ");
fwrite($fi, "\n");
fwrite($fi,$nom);
fwrite($fi, "\n");
fwrite($fi,$com);
fwrite($fi, "\n");
fwrite($fi, "----------------------------------------------------- \n \n");
fclose($fi);
echo "Archivo guardado";
?>

What happens is that it always generates the message that goes in the case of the or die.

Thank you very much for the help

    
asked by J. Zuleta 18.04.2017 в 06:48
source

1 answer

-1

The name of the file is poorly defined.

As indicated in the fopen documentation the file path must be complete not only the name of the file file, not finding the route goes to or die .

The right thing should be like this:

fopen("c:\archivo.txt", "a") or die
    
answered by 18.04.2017 / 08:17
source