20,359
社区成员
发帖
与我相关
我的任务
分享
fwrite将字符写入指定的文件
$file=file_get_contents('a.txt');
echo $file;
$string="aaa";
echo strpos($file, 'a');
if(strpos($file, 'a')) {
echo "你要插入的字符串已经存在";
}else{
$fp=fopen("a.txt", "w+");
$string=$file.":".$string;
fwrite($fp, $string);
}
$file=file_get_contents('a.txt');
echo $file;
?>
<?php
if(!file_exists("ceshi.txt")){
fopen("ceshi.txt",'w');
}
$handle = fopen("ceshi.txt",'a');
//获得浏览器地址
$url = "http://".$_SERVER["HTTP_HOST"].(($_SERVER["SERVER_PORT"]==="80")?"":$_SERVER["SERVER_PORT"]).$_SERVER["REQUEST_URI"];
echo $url."<br>";
$txt = file_get_contents("ceshi.txt");
//写入文件
if (!empty($txt)){
if (preg_match("#".$url."#",$txt)==0){
fwrite($handle,$url);
}else{
echo "已存在地址";
}
}else{
fwrite($handle,$url);
}
?>