Home >Backend Development >PHP Tutorial >PHP simply implements a method to generate txt files to a specified directory

PHP simply implements a method to generate txt files to a specified directory

高洛峰
高洛峰Original
2017-02-04 16:41:443444browse

The example in this article describes how to simply implement PHP to generate a txt file to a specified directory. Share it with everyone for your reference, the details are as follows:

<?php
//fopen第二个参数可以是以下四个,区别就是是清空内容再写还是在后面累加内容
//"w" 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
//"w+" 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
//"a" 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
//"a+" 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
$myfile = fopen("c:/newfile.txt", "a") or die("Unable to open file!");//这个是在c盘根目录生成文件
$txt = $_GET;//获取参数
$b="";
foreach($txt as $k=>$v){
  $b = $b ."[".$k."]=>".$v."\n";
}
$txt1 = $_POST;
foreach($txt1 as $k=>$v){
  $b = $b ."[".$k."]=>".$v."\n";
}
$b = $b."123";
fwrite($myfile, $b);//写入内容,可以写多次哦,不过没啥意义,因为你拼接好字符串,一次写入就行了
fclose($myfile);//关闭该操作
?>

A simple and useful method is

file_put_contents("c:/zll.txt","内容");

I hope this article will be helpful to everyone in PHP programming.

For more related articles on how to simply generate txt files to a specified directory using PHP, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn