【问题标题】:Adding Text to End of the File PHP [duplicate]将文本添加到文件 PHP 的末尾 [重复]
【发布时间】:2013-11-25 18:38:41
【问题描述】:

我有这个 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 ?我正在将它用于我的博客,我只是注意到了这个小问题。

【问题讨论】:

标签: php


【解决方案1】:

你可以使用

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 我正要补充一点:-p
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2016-08-15
    • 1970-01-01
    • 2014-08-04
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多