PHP Write string to new line for the first time and not at the end of file

那年仲夏 提交于 2019-12-24 03:06:46

问题


Using Codeigniter function write_file(), I can write to an existing file like this:

write_file("path_to_file", "String four"."\n", "a+");

Assume I already have a file like this:

String one
String two
String three

With write_file("path_to_file", "String four"."\n", "a+") , I have the ouput below when writing to file for the first time:

String one
String two
String three String four

Any idea about how I can write String four to new line for the first time? Like shown below:

String one
String two
String three 
String four

Thanks.


回答1:


Not knowing much about this write_file() function, but given what you said, this should work :

write_file("path_to_file", "\n"."String four"."\n", "a+")



回答2:


Try adding new line before String four:

write_file("path_to_file", "\nString four\n", "a+")


来源:https://stackoverflow.com/questions/18322558/php-write-string-to-new-line-for-the-first-time-and-not-at-the-end-of-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!