PHP有自带的高性能函数 var_export
conn.php
php
$dbconfig = array (
'name'='root',
b.php
// 读取配置
include 'conn.php';
echo $dbconfig['host'];
// 修改配置
$dbconfig['host'] = 'xxx.xxx.xxx.xxx';
file_put_contents('conn.php', "?php\n$dbconfig = " . var_export($dbconfig) . "\n?");
// 再读取配置
参考连接:
使用form表单post数据到PHP,然后用file_put_contents($fileName, $data)写入文件,$fileName是文件名,$data是要写入的数据
可能会有一个notice的报错,不必理会
$data = $_POST['text'];
$fileName = 'a.txt';
file_put_contents($fileName, $data);
!doctype html
html
head
titletest/title
/head
body
form action="./a.php" method="post"
input type="submit" value="提交"
/form
/body
/html
//记录返回值
? ? ? $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());
//项目路径目录,判断是否存在,不存在则创建
? ? ? if(!is_dir($lujing)){
? ? ? }
//文件,判断是否存在,不存在则创建
? ? ? //以读写方式打写指定文件,如果文件不存则创建
? ? ? 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);
? ? ? ? ? fclose ($TxtRes); //关闭指针
通过fwrite
$file = fopen("test.txt","a+"); //次方法会自动生成文件test,txt,a表示追加写入,
//w代表替换写入fwrite($file,"写入代码");fclose($file);
file_put_content()方法写入
file_put_contents("test.txt","奥斯卡老\r\n顿积分");//这里说一下\r\n在双引号下
//才会换行如果单引号就识别不了
//如果想追加写入内容,这个函数还有第三个参数FILE_APPEND
PHP向MySQL数据库中写入数据有三个步骤:
①.,PHP和MySQL建立连接关系
代码如下
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
当然,前提是已经安装WEB服务器、PHP和MySQL,并且建立MySQL表"cnbruce"
mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码
然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
补充:读取表
读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.
while($row = mysql_fetch_array($result))
{
echo $row['Topic'] . "br/";
首先,把PHP数组中的数据写入JSON文件.
//?生成一个PHP数组
$data?=?array();
$data['a']?=?'test';
$data['b']?=?'bbb';
//?把PHP数组转成JSON字符串
$json_string?=?json_encode($data);
//?写入文件
file_put_contents('test.json',?$json_string);
然后,把JSON文件中的数据读取到PHP变量中.
//?从文件中读取数据到PHP变量
$json_string?=?file_get_contents('test.json');
//?把JSON字符串转成PHP数组
$data?=?json_decode($json_string,?true);
//?显示出来看看
var_dump($data);
以上就是土嘎嘎小编为大家整理的php写入文件数据相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!