Home > Backend Development > PHP Tutorial > Summary of PHP file operation methods

Summary of PHP file operation methods

韦小宝
Release: 2023-03-19 19:28:01
Original
1488 people have browsed it

After we have learned the relative basics of PHP, we will learn how to operate PHP files. There are many ways to operate files. We need to open the PHP file, add, delete, modify, and check the PHP file. Finally, we need to close it. PHP file, let’s not talk too much nonsense, let’s take a look together! !

Write data in the data file:

 <?php
 /**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
 header("Content-type: text/html; charset=utf-8");
 //write data
 $f = fopen(&#39;data&#39;,&#39;w&#39;);//打开文件
 fwrite($f,&#39;Hello PHP&#39;);//写入数据
 fclose($f);//关闭文件
 echo &#39;OK&#39;;
 //windows环境暂时不考虑权限问题
Copy after login

After successful writing, you can see "OK" on the page

Next read the data in the data file Data

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
header("Content-type: text/html; charset=utf-8");

//read data
$f = fopen(&#39;data&#39;,&#39;r&#39;);
$content = fgets($f);
echo $content;
fclose($f);
Copy after login

How to read if there are multiple rows of data?

Method 1 while:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/29
 * Time: 17:05
 */
header("Content-type: text/html; charset=utf-8");
$f = fopen(&#39;data&#39;,&#39;r&#39;);
//读取多行数据 while
while(!feof($f)){//feof() 函数检测是否已到达文件末尾
  $content = fgets($f);
  echo $content;
}
fclose($f);
Copy after login

Method 2file_get_contents():

echo file_get_contents(&#39;data&#39;);
Copy after login

The above is all the contents of all operations on PHP files. For operations Students who are not familiar with PHP files should practice more! !

Sublimated knowledge:

What are the commonly used PHP file functions

Simply organize some common functions for operating PHP files: filetype() Get file type function is_dir() Determine whether the given file name is a directory...

How to open a php file? How to run php file?

Recently, many friends have asked us, what is php file, how to open php file and other questions. Here is some information compiled by the editor...

phpAbout the usage summary of PHP file directories

The directory of PHP files is very important. The php Chinese website has collected several articles about the usage of PHP file directories. I hope it will help everyone understand how to obtain the file directories. It helps, let’s take a look

The above is the detailed content of Summary of PHP file operation methods. 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