Home >Backend Development >PHP Tutorial >怎么建一串字符写入某文件的开头

怎么建一串字符写入某文件的开头

WBOY
WBOYOriginal
2016-06-13 10:03:43993browse

如何建一串字符写入某文件的开头
假设某文件为a.php
内容为:JJJJJJJJJJJJJJJJJJJJJJJ;
通过程序使a.php 文件开头插入一串字符串的 字符串+JJJJJJJJJJJJJJJJJJJ

------解决方案--------------------
最简单的做法先把文件内容用file_get_contents()读到个字符串里
再在这个字符串前加上你要加的字符串
然后再写回文件a.php
麻烦点的做法就是用fopen(),参数r+
------解决方案--------------------
呵呵
$temp=file_get_contents("a.php");
$add_str="要添加的字符串";
file_put_contents("a.php",$add_str.$temp);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn