能实现读取 修改 保存就行
第1个回答 推荐于2016-11-17
/**
* 读文件
**/
function read_file($filename)
{
$fp = fopen($filename, "r") or die("couldn't open $filename");
$read = fread($fp, filesize($filename));
fclose($fp);
return $read;
}
/**
* 写文件
**/
function write_file($filename, $buffer)
{
$fp = fopen($filename, "w") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
$write = fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 修改(只是追加内容)
**/
function append_to_file($filename, $buffer)
{
$fp = fopen($filename, "a") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 测试
**/
$str = read_file('test.txt');
echo $str;
write_file('test2.txt', $str);
append_to_file('test2.txt', "ABCD");
其实,读文件有更简便的方法,你可以看看 file 和 file_get_contents 函数。
写文件也有现成的 file_ put_ contents 函数。本回答被提问者采纳
* 读文件
**/
function read_file($filename)
{
$fp = fopen($filename, "r") or die("couldn't open $filename");
$read = fread($fp, filesize($filename));
fclose($fp);
return $read;
}
/**
* 写文件
**/
function write_file($filename, $buffer)
{
$fp = fopen($filename, "w") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
$write = fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 修改(只是追加内容)
**/
function append_to_file($filename, $buffer)
{
$fp = fopen($filename, "a") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 测试
**/
$str = read_file('test.txt');
echo $str;
write_file('test2.txt', $str);
append_to_file('test2.txt', "ABCD");
其实,读文件有更简便的方法,你可以看看 file 和 file_get_contents 函数。
写文件也有现成的 file_ put_ contents 函数。本回答被提问者采纳
第2个回答 2010-02-18
$content = file_get_contents($file); //读文件
$content = $content . '正在修改'; //修改文件
file_put_contents($file, $content); //保存文件
$content = $content . '正在修改'; //修改文件
file_put_contents($file, $content); //保存文件