php 读取修改txt文件

//txt文件中只有一行数据 读
$fp = fopen("password.txt", "r");
if($fp)
{
     $pwd = fgets($fp);
}
else
{
     echo "打开文件失败";
}
fclose($fp); 
View Code

//txt文件中只有一行数据 写

 1 $fp = fopen("password.txt", "w");//文件被清空后再写入
 2 if($fp)
 3 {
 4      $flag=fwrite($fp,$password);
 5      if(!$flag)
 6      {
 7           echo "写入文件失败<br>";
 8           break;
 9      }
10 }
11 fclose($fp); 
View Code

//txt文件中有多行数据  读 以数组的形式

 1 $path="wifi_customer_settings.txt";
 2 $body = file_get_contents($path);
 3 if( file_exists( $path ) )
 4 {
 5     $body = file_get_contents($path);//转为数组
 6 //echo "<script language=javascript>alert('文件存在');</script>";
 7 }
 8 else
 9 {
10    // echo "<script language=javascript>alert('文件不存在 $path');</script>";
11 }
12 $cbody = file($path);
13 //print_r($cbody);
14 $wifissid=$cbody[0];
15 $wifipwd=$cbody[1];
16 $wifissid=str_replace('wifiSSID=','' ,$wifissid);//文本替换 将$wifissid值的wifiSSID替换为' '
17 $wifipwd=str_replace('wifiPWD=','' ,$wifipwd);
View Code

//txt文件中有多行数据 写  以数组的形式

1 $path="wifi_customer_settings.txt";
2 $wifissid2=$_POST['wifissid'];
3 $wifipwd2=$_POST['wifipwd'];
4 $arr=array("wifiSSID=".$wifissid2,"\n","wifiPWD=".$wifipwd2);
5 $fp=fopen($path, 'w');
6 fputs($fp,$arr[0]); 
7 fputs($fp,$arr[1]);
8 fputs($fp,$arr[2]);
9 fclose($fp);  
View Code

 

转载于:https://www.cnblogs.com/JoanLin-workNotes/p/4321850.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值