Forum Moderators: coopster
Sample code:
<?php if (isset($_POST['submit'])){
$statusfile = fopen("texts/text.txt", 'r+') or die("can't open file");
fwrite($statusfile, $_POST['profile']."\n");
}?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input name="submit" type="submit" value="Submit">
</form>
What's wrong with code?
Isn't the "r+" parameter to open file for reading and writing, with pointer at the begginging of the file?
<?php
if (isset($_POST['submit'])){
$file = "texts/text.txt";
$statusfile = fopen($file, 'r+') or die("can't open file");
$existing_file = fread($statusfile, filesize($file));
fwrite($statusfile, $_POST['profile']."\n".$existing_file);
fclose($statusfile); // Don't forget to close!
}?>
exactly, it isn't a prepend, it just writes over whatever is there.
you could even do it this way
<?php
if (isset($_POST['submit'])){
$file = "texts/text.txt";
$existing_file = file_get_contents($file);
$statusfile = fopen($file, 'w') or die("can't open file");
fwrite($statusfile, $_POST['profile']."\n".$existing_file);
fclose($statusfile); // Don't forget to close!
}?>