search
HomeBackend DevelopmentPHP TutorialDetailed explanation of examples of file operations in php
Detailed explanation of examples of file operations in phpJun 23, 2017 am 11:40 AM
filephpsentenceoperatedocumentread

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;

4.Close File

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

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>

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 "
";4 for($i = 0;$i ";6 }7 echo "
".$arr[$i]."
";

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;

***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;

***is_file determines whether the file exists

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

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 of the file copy

 copy("pv.txt","pv2.txt");

File rename rename

rename("pv2.txt","pv5.txt");

Delete File unlink

 unlink("pv5.txt");

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!

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
鸿蒙原生应用随机诗词鸿蒙原生应用随机诗词Feb 19, 2024 pm 01:36 PM

想了解更多关于开源的内容,请访问:51CTO鸿蒙开发者社区https://ost.51cto.com运行环境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、创建应用点击File->newFile->CreateProgect。选择模版:【OpenHarmony】EmptyAbility:填写项目名,shici,应用包名com.nut.shici,应用存储位置XXX(不要有中文,特殊字符,空格)。CompileSDK10,Model:Stage。Device

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

使用java的File.renameTo()函数重命名文件使用java的File.renameTo()函数重命名文件Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函数重命名文件在Java编程中,我们经常需要对文件进行重命名的操作。Java提供了File类来处理文件操作,其中的renameTo()函数可以方便地重命名文件。本文将介绍如何使用Java的File.renameTo()函数来重命名文件,并提供相应的代码示例。File.renameTo()函数是File类的一个方法,

使用java的File.getParentFile()函数获取文件的父目录使用java的File.getParentFile()函数获取文件的父目录Jul 27, 2023 am 11:45 AM

使用java的File.getParentFile()函数获取文件的父目录在Java编程中,我们经常需要操作文件和文件夹。当我们需要获取文件的父目录时,可以使用Java提供的File.getParentFile()函数来完成。本文将介绍如何使用这个函数并提供代码示例。Java中的File类是用于操作文件和文件夹的主要类。它提供了许多方法来获取和操作文件的属性

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!