PDA

View Full Version : [PHP] Writing to text files [+REPS!!]



Decode
29-02-2008, 03:41 PM
I know how to write to a text file;


<?php
$FormData = $_POST["FormData"];
if (!isset($_POST['submit'])) {
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Data:<br>
<input type="text" style="width: 400px;" name="FormData"><br>
<br><br>
<input type="submit" value="submit" name="submit" size="50">
</form>
<?
} else {
$submit = fopen("data.txt", "a");
fputs ($submit,implode,("\n"));
fwrite($submit,"$FormData");
fclose($submit);
echo "Done!";
}
?>

But how do i change it so that the stuff writen from the form is at the top of the page rather than the bottom.

+Rep for help

Tom

RYANNNNN
29-02-2008, 04:02 PM
You have one if/else statement so it's easy... Look at it closely, current its saying if the form HASNT been posted, show the form. So, to get the PHP at the top, you wanna do if the form HAS been posted, do the PHP, and then if it has been posted then the only other else is HASNT so you show the form

Eccentric
29-02-2008, 06:02 PM
You have one if/else statement so it's easy... Look at it closely, current its saying if the form HASNT been posted, show the form. So, to get the PHP at the top, you wanna do if the form HAS been posted, do the PHP, and then if it has been posted then the only other else is HASNT so you show the form
which is what he wants, try chmod'in the text file to 777?

Decode
29-02-2008, 06:09 PM
Its not a problem with writing the data from the form... what happens is when someone submits some data it goes to the bottom of data.txt. How do i change it so it adds it to the top of data.txt?

Florx
29-02-2008, 06:15 PM
Ok fopen the file then clear the fille and add to the file your data and the data from fopen. That will put it at the top.

RYANNNNN
29-02-2008, 06:34 PM
which is what he wants, try chmod'in the text file to 777?

I misread, atleast I actually tried to help because I know what I'm talking about other than giving an answer which you obviously guessed and have no clue about.

But yeah, you need to open the file first, store whats in it in a variable, and then write $yourtext . $originaltext to the file.

Decode
29-02-2008, 06:44 PM
I misread, atleast I actually tried to help because I know what I'm talking about other than giving an answer which you obviously guessed and have no clue about.

But yeah, you need to open the file first, store whats in it in a variable, and then write $yourtext . $originaltext to the file.
Thanks :)

+Rep

Agnostic Bear
29-02-2008, 07:07 PM
Or you can just use the mode r+, simple as.

Mentor
01-03-2008, 05:53 PM
So basicly you want it to append the top of the file, rather than the bottom with the new data?

Php doesnt (i dont think) have a function for this but its still quite easy to do.


// Store the current file in to a varible.
$fileinfo = file_get_contents("data.txt");
//set are info out so are new data is at the top.
$newdata = $FormData + "\n" + $fileinfo;
//Overwrite are file with the new one
file_put_contents ("data.txt", $newdata);
echo "Done!";

lolwut
02-03-2008, 12:33 PM
<?php
$filename = "file.txt";
////////////////////////
$current = file_get_contents($filename);
$new = $_POST['FormData'] . "\n" . $current;
file_put_contents($filename,$new);
?>
Not tested.

http://us3.php.net/file_put_contents
http://us3.php.net/file_get_contents

Want to hide these adverts? Register an account for free!