将文本添加到文件末尾PHP

时间:2013-11-13 08:41:15

标签: php

我有这个PHP脚本:

<?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');
}
?>

它完美无缺,但它有一个小问题。文本将添加到文件末尾。有没有办法让它在文本文件的开头添加$ savestring?我在我的博客上使用它,我只是注意到这个小问题。

2 个答案:

答案 0 :(得分:2)

您需要使用正确的书写模式:

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

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

答案 1 :(得分:0)

你可以使用

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