Read and write to a file. txt in php without using BD [Solved]

Asked

Viewed 704 times

-1

inserir a descrição da imagem aquiI’m creating a chat system to implement in a project, and I’m having trouble recording and rescuing within a file .txt, when I manually write to the file it opens, I’m just not able to write to the system, and in the html console no errors appear

follows the codes created.

Index.js

function tempo(){

    setInterval(ler,3000);

};

function ler(){

    $("#conversa").load("ler.php");


             }; 

function escrever(){

    var mensagens = "textarea"; 
    var usuario = "imput:text"; 
    $.ajax({    type:"POST",
        url:"escrever.php",
        data:{"mensagens":mensagens, "usuario":usuario},
        success:function(){
            ler();
        }});} 

write.php

<?php 



$file = file_get_contents("chat.txt");

$mensagens = "<b>" $_POST['usuario']. "</b> diz: ".$_POST['mensagens']."<br>";
$file = file_put_contents("chat.txt", $file; $mensagens);



?> 

read php.

<?php


    $file = file_get_contents("chat.txt");
    print $file;

?>
  • 1

    This var usuario = "imput:text"; misspelled, already tested?

  • my input in index is like this: <input type="text" id="text" class="form-control" placeholder="User"/> ai put like this : var usuario = "text"; that would be it ?

  • No, you wrote input with m... Fix this and play on the console or an Alert to see if it’s picking up the text there...

  • even so it is giving error, after which I answered I realized the error

1 answer

1


Use the functions fopen and fwrite.

<?php

$meu_arquivo = 'seuArquivo.txt';
$handle = fopen($meu_arquivo, 'a') or die('Erro ao abrir o arquivo:  '.$meu_arquivo);
$texto = 'texto 1 ';
fwrite($handle, $texto);
$texto_novo = "\r\n".'texto 2';
fwrite($handle, $texto_novo);

?>

EDITED

To test created these 2 files in the same directory, when opening the page index.php the data will be sent via post and an alert will be displayed confirming the upload, then you can check in the same directory where these two php files that the file seuArquivo.txt will be created:

index php.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>

    <script>
        function escrever(){
            var mensagens = $('textarea#mensagens').val();  
            var usuario = $('#text').val();
            $.ajax({    type:"POST",
                url:"escrever.php",
                data:{"mensagens":mensagens, "usuario":usuario},
                success:function(){
                    alert("Dados enviados");
                }
            });
        }
        escrever();
    </script>
</body>
</html>

write.php

<?php
    $mensagens = "<b>".$_POST['usuario']. "</b> diz: ".$_POST['mensagens']."<br>";
    $meu_arquivo = 'seuArquivo.txt';
    $handle = fopen($meu_arquivo, 'a') or die('Erro ao abrir o arquivo:  '.$meu_arquivo);
    fwrite($handle, $mensagens);
?>
  • I’ll try friend

  • would look like this : <pre><code> <?php $file = 'chat.txt'; $Handle = fopen($fiile, 'a') or die('Error opening the file: '.$file); $messages = "<b>" $_POST['user']. " </b> says: ". $_POST['messages']." <br>"; fwrite($Handle, $messages); $texto_novo = "r n". 'text 2'; fwrite($Handle, $texto_novo); ? > <code>

  • @Fabrizzio That, just corrected there in $mensagens = "<b>" $_POST['usuario']. "</b> diz: ".$_POST['mensagens']."<br>"; you’re not concatenating the "<br>" with the $_POST["usuario"]., said put a . among them.

  • still not recording, and not giving error in the console

  • @Fabrizzio, I edited my answer including the test I did here, the file is generated without problem, try to copy the code I just put and perform the same test.

  • still not recording in txt friend, appears spam file sent and everything, but does not record, must be something else that I can not explain to you, I can track the system by email?

  • @Fabrizzio, check, that witchcraft in rsrsrs. Let me just confirm one thing, the folder where you put the files index.php and escrever.php apache or Nginx (depends on which one you are using to serve the php files) has the right to write to it right? Because maybe they only have the right to read and not write. so you can’t create files in it.

  • @Fabrizzio, ok, is asking for permission to access the file on googledrive

  • I’m using wampserve apache, will that be? has how to edit and give permission?

  • @Fabrizzio, I answered you in the email, basically the only problem was that in the file write.php you left your old code, and it was with syntax error, just leave it as I put in the answer here from the stack.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.