如何在不删除文件的所有其他内容的情况下在 php 中向文件中写入新行?
<?php
if(isset($_POST['songName'])){
$newLine = "\n";
$songName = $_POST['songName'];
$filename = fopen('song_name.txt', "wb");
fwrite($filename, $songName.$newLine);
fclose($filename);
};
?>
这是文件的样子
Current view
这应该看起来像 Ideal View
最佳答案
简单地:
file_put_contents($filename,$songName.$newLine,FILE_APPEND);
负责打开、写入和关闭文件。如果需要,它甚至会创建文件! ( see docs )
如果您的新行不起作用,则问题在于您的
$newLine
变量,而不是文件附加操作。以下方法之一将起作用:$newLine = PHP_EOL; << or >> $newLine = "\r\n";
关于php - 用 PHP 编写一个新文件并附加一个文件而不删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38841280/