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

PHP simply implements a method to generate txt files to a specified directory_php tips

WBOY
Release: 2016-05-16 19:53:49
Original
1978 people have browsed it

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

<&#63;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);//关闭该操作
&#63;>

Copy after login

Easy and easy to use method is

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

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP file operations", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax tutorial", "Summary of PHP operating office document skills (including word, excel, access, ppt)" , "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", " php mysql database operation introductory tutorial" and "php common database operation skills summary"

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template