Dallox
Dallox

Reputation: 487

PHP writing a text file in the begin

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>

Upvotes: 1

Views: 1505

Answers (4)

itsid
itsid

Reputation: 811

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

Upvotes: 0

<?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.

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

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

Upvotes: -1

Derk Arts
Derk Arts

Reputation: 3460

Check the fopen 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.

Upvotes: 1

Related Questions