Detailed explanation of examples of file operations in php

零下一度
Release: 2023-03-10 16:16:01
Original
1194 people have browsed it

Add utf-8 in php:

1 header("Content-type:text/ html;charset='UTF-8'");

File operation steps:

1. Create in the same directory A folder of file.txt

2. Open the file

1 $res = fopen("file.txt","r");//Open the file path. After opening, it is a resource and needs further processing;//r It means read-only

3. Read the file

$str= fread($res,300);//第二个参数为读取的长度(每个汉字的长度为3)$str = fread($res,filesize("file.txt"));//filesize读取文件大小
//以上二者选其一显示echo $str;
Copy after login

4.Close File

fclose($res);//读取完毕后,关闭资源
Copy after login

Reading of multi-line sentences:

1. Set the text to Can read and write, and write multi-line text

2. Read one line or read multiple lines

<span style="font-size: 16px"><span style="color: #008080">1 //打开文件<br>2</span> <span style="color: #800080">$res2</span> = <span style="color: #008080">fopen</span>("file.txt","r"<span style="color: #000000">);</span><span style="color: #008080">3</span> <span style="color: #800080">$str2</span> = <span style="color: #008080">fgets</span>(<span style="color: #800080">$res2</span>);<span style="color: #008000">//</span><span style="color: #008000">到此出现第一行</span><span style="color: #008080">4</span> <span style="color: #0000ff">echo</span> <span style="color: #800080">$str2</span>."<br>";<span style="color: #008000">//</span><span style="color: #008000">换行</span><span style="color: #008080">5</span> <span style="color: #800080">$str2</span> = <span style="color: #008080">fgets</span>(<span style="color: #800080">$res2</span><span style="color: #000000">);</span><span style="color: #008080">6</span> <span style="color: #0000ff">echo</span> <span style="color: #800080">$str2</span>;<span style="color: #008000">//</span><span style="color: #008000">到此出现第二行<br>7 //使用while循环可以使其全部显示(如下)<br>8 while($str2 = fgets($res2)) {<br>9   echo $str2."<br>";<br>10 }<br>11 //关闭文件<br>12 fclose($res2);<br></span></span>
Copy after login

The file() function reads the file into an array, with each element separated by a newline character:

1 $arr = file("file.txt");2 print_r($arr);3 echo "<table border = 1>";4 for($i = 0;$i < count($arr);$i++) {5     echo"<tr><td>".$arr[$i]."</td></tr>";6 }7 echo "</table>";
Copy after login

The file_get_contents() function reads the file contents into a string (Able to achieve cross-domain reading):

1 $str4 = file_get_contents("http://www.jd.com");//可以跨域2 echo $str4;
Copy after login

***file_put_contents() function writes a string to a file, and Calling fopen(), fwrite(), fclose() in sequence has the same function;

1 $bol = file_put_contents("file.txt","我爱你");//后面的内容可以将前面内容全部覆盖2 echo $bol;
Copy after login

***is_file determines whether the file exists

1 $bol = is_file("file3.txt");//判断file3.txt是否存在2 echo $bol;
Copy after login

Statistics website pv (number of visits) through the above mark (***):

 1 //首先判断有没有统计的文件 2 if(is_file("pv.txt")) {//有 3     //取文件里面的值 4     $res = file_get_contents("pv.txt"); 5     //累加 6     $res += 1; 7     //类加后的值存进去 8     file_put_contents("pv.txt",$res); 9     //输出pv数10     echo file_get_contents("pv.txt");11     }else {//没有统计的文件12     //创建文件,同时给文件里一个初始值13     file_put_contents("pv.txt",1);14     //输出一下当前的pv是:115     echo file_get_contents("pv.txt");16 }
Copy after login

Copy of the file copy

 copy("pv.txt","pv2.txt");
Copy after login

File rename rename

rename("pv2.txt","pv5.txt");
Copy after login

Delete File unlink

 unlink("pv5.txt");
Copy after login

The above is the detailed content of Detailed explanation of examples of file operations in php. For more information, please follow other related articles on the PHP Chinese website!

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