php讲数据写入文件 php把数据写入文本

php将数据写入文件使用form表单post数据到PHP,然后用file_put_contents($fileName, $data)写入文件,$fileName是文件名,$data是要写入的数据
新建一个a.php文件,将下面的复制进去访问一下,填写后点击提交,会生成一个a.txt的文件,里面是你填写的内容
可能会有一个notice的报错,不必理会
?php
$data = https://www.04ip.com/post/$_POST['text'];
$fileName = 'a.txt';
file_put_contents($fileName, $data);
?
!doctype html
html
head
meta charset="utf-8"
titletest/title
/head
body
form action="./a.php" method="post"
textarea name="text" id="" cols="30" rows="10"/textarea
input type="submit" value="https://www.04ip.com/post/提交"
/form
/body
/html
PHP将数据写入txt文件//记录返回值
$write_data_a = [
'html_url'=$getUrl,
'ip'= $this-get_real_ip(),
'time'=date("Y-m-d H:i:s",time()),
'res'= $response
];
//转化为JSON
$write_data_a = json_encode($write_data_a) . '||' . "\n";
$date = date("Y-m-d", time());
//项目路径目录,判断是否存在,不存在则创建
$lujing = "./360_mobile_res_sd";
if(!is_dir($lujing)){
mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);
}
//文件 , 判断是否存在,不存在则创建
$TxtFileName = "./360_mobile_res_sd/" . $date . "_2.txt";
//以读写方式打写指定文件,如果文件不存则创建
if(file_exists($TxtFileName))
{
//存在,追加写入内容
file_put_contents($TxtFileName, $write_data_a, FILE_APPEND);
}
else
{
//不存在,创建并写入
if( ($TxtRes=fopen ($TxtFileName,"w ")) === FALSE){
exit();
}
if(!fwrite ($TxtRes,$write_data_a)){ //将信息写入文件
fclose($TxtRes);
exit();
}
fclose ($TxtRes); //关闭指针
}
PHP json数据写入到json文件,读取json文件// 生成一个PHP数组
$data = https://www.04ip.com/post/array();
$data['name'] = 'admin';
$data['pwd'] = '123456';
// 把PHP数组转成JSON字符串
$json_string = json_encode($data);
// 写入文件
file_put_contents('user.json', $json_string);
// 从文件中读取数据到PHP变量
$json_string = file_get_contents('user.json');
// 把JSON字符串转成PHP数组
$data = https://www.04ip.com/post/json_decode($json_string, true);
// 显示出来看看
var_dump($data)
【php讲数据写入文件 php把数据写入文本】关于php讲数据写入文件和php把数据写入文本的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读