0

I've got this PHP script:

<?php
if ('POST' === $_SERVER['REQUEST_METHOD'] && ($_POST['title'] != 'Title') && ($_POST['date'] != 'Date'))
{
    $fileName = 'blog.txt';
    $fp = fopen('blog.txt', 'a');
    $savestring = PHP_EOL . "<h2><center><span>" . $_POST['title'] . "</span></center></h2>" . "<div class=fright><p><em>|<br><strong>| Posted:</strong><br>| " . $_POST['date'] . "<br>|</p></em></div></p></em>" . "<p><em>" . $_POST['paragraph'] . "</em></p>" . PHP_EOL . "<hr>";
    fwrite($fp, $savestring);              
    fclose($fp);
    header('Location: http://cod5showtime.url.ph/acp.html');
}
?>

It works perfectly but it has a slight problem. The text is added at the end of the file. Is there a way to make it add the $savestring at the beginning of the text file ? I'm using this for my blog and I just noticed this slight problem.

2
  • Hey there is a similarly question, which will help you: stackoverflow.com/questions/1760525/… Commented Nov 13, 2013 at 8:45
  • The highest rated answer from that question takes an unnecessarily heavy handed approach. Commented Nov 13, 2013 at 8:46

2 Answers 2

2

You need to use the correct writing mode:

$fp = fopen('blog.txt' 'c');

https://www.php.net/manual/en/function.fopen.php

Sign up to request clarification or add additional context in comments.

1 Comment

I was just about to add that :-p
0

You can you use

$current = file_get_contents($file);
// Append a data to the file
$current .= "John Smith\n";
file_put_contents($file, $current, FILE_APPEND | LOCK_EX);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.