PHP writing a text file in the begin

999

So we are making in the class a sort of log. There is a input box and a button. Everytime the button is pressed, PHP will write on the text file and prints the current log. Now the text appears on the bottom, and we need to have the text appear on the top. Now how would we do that?

We tried doing this with alot of my classmates but it all resulted in weird behavours. (Like text is printed more then once, etc)

Thanks alot!

EDIT: Sorry, here is the code:

<html lang="en">
<head>
    <title>php script</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
    <input type="text" name="text"/>
    <input type="submit" value="Submit" />
    <?php
        //Basic variables
        echo("<br/>");
        $myFile = "log.txt";
        $logfile = fopen($myFile,'r+');
        $theData = fread($logfile,filesize($myFile));

        //Cookie stuff so the username is rememberd.
        $username = $_COOKIE['gebruikerscookie'];;
        if(isset($_POST['username'])){
            $gebruiker = $_POST['username'];
            if($_COOKIE['gebruikerscookie'] == $gebruiker){
                $username = $_COOKIE['gebruikerscookie'];
                echo("Welcome back");
            }else{
                setcookie("gebruikerscookie", $gebruiker);
                $username = $_COOKIE['gebruikerscookie'];
                echo("Welcome dude!");
            }           
        }

        //Checks if theres something inside 
        if(isset($_POST['text'])){
            $message = "<br/>". $username ." : " . $_POST['text'];
            fwrite($logfile, $message ,strlen($message));
        }
        echo($theData);
    ?>
    </form>
</body>

185

Answer

Solution:

Check thefopen manual on modes: http://www.php.net/manual/en/function.fopen.php

Try'r+' Open for reading and writing; place the file pointer at the beginning of the file.

Altough without any code this is hard to answer.

831

Answer

Solution:

<?php
$contentToWrite = "Put your log content here \n";
$contentToWrite .= file_get_contents('filename.log');
file_put_contents('filename.log', $file_data);
?>

This will add the previous content of your file after your cureent content and write on your file.

Please reply if you have any doubt.

895

Answer

Solution:

you're just missing the

fclose();

I assume, since not closing a filehandle can cause a lot of strange errors like this.

So

$myFile = "log.txt";
$logfile = fopen($myFile,'r+');
........
//Checks if theres something inside 
if(isset($_POST['text'])){
   $message = "<br/>". $username ." : " . $_POST['text'];
   fwrite($logfile, $message ,strlen($message));
}
fclose($logfile); // close it outside the if-condition!
echo($theData);

should do the trick

876

Answer

Solution:

$log = file('log.txt');
krsort($log);
foreach($log as $line) echo "$line<br>\n";

People are also looking for solutions to the problem: php - How to replace a whole div with an other with preg_replace regEx

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.