使用form表单post数据到PHP,然后用file_put_contents($fileName, $data)写入文件,$fileName是文件名,$data是要写入的数据
可能会有一个notice的报错,不必理会
php
$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
// 生成一个PHP数组
$data = array();
$data['name'] = 'admin';
// 把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 = json_decode($json_string, true);
// 显示出来看看
var_dump($data)
php导出数据excel有专门的库,当导出少量数据的时候速度很快,但是当数据量大的时候就会存在服务器内存不够之类的.
所以在导出大量数据的时候就应该分页查询数据,避免服务器宕机.正好PHP提供了fputcsv函数可以将数据写入到csv文件中.
这样我们就可以使用PHP对数据进行分页查询,再写入到csv文件中.
//记录返回值
? ? ? $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); //关闭指针
以上就是土嘎嘎小编为大家整理的php大数据写入文件相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!