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.

Harish Singh
  • 3,359
  • 5
  • 24
  • 39
  • Hey there is a similarly question, which will help you: http://stackoverflow.com/questions/1760525/need-to-write-at-beginning-of-file-with-php – schnawel007 Nov 13 '13 at 08:45
  • The highest rated answer from that question takes an unnecessarily heavy handed approach. – shanethehat Nov 13 '13 at 08:46

2 Answers2

2

You need to use the correct writing mode:

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

http://us1.php.net/manual/en/function.fopen.php

shanethehat
  • 15,460
  • 11
  • 57
  • 87
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);
vijaykumar
  • 4,658
  • 6
  • 37
  • 54