Home  >  Article  >  Backend Development  >  ​Complete file operations in one day and related operations of reading files (with detailed explanation)

​Complete file operations in one day and related operations of reading files (with detailed explanation)

慕斯
慕斯Original
2021-06-08 16:58:282221browse

The previous article introduced you to "5 minutes to understand what is a file system?" What types of functions do they need to create? 》, this article will continue to introduce to you the related operations of reading files after finishing the file operation in one day, let’s work hard together! ! !

​Complete file operations in one day and related operations of reading files (with detailed explanation)

Whether the file is executable:

- is executable()

Whether the file is readable:

is readable()

Whether the file is writable:

is writable()

Creation time of the employment file:

filectime()返回创建时间的时间戳

Modification time of the employment file:

一filemtime()

Last access time of the obtained file:

- fileatine()

flock simple file lock

Modification time of the obtained file

filemtime()

Get the file last access time

fileat: ime()

flockSimple file lock

Open file

fopen()

Parameter: The path of the file to be opened

Parameter: The way to open the file r Open the file in read-only mode

Return value: Return the resource successfully, return false

Close the file:

fclose()

Parameter: the resource returned by fopen successfully opening the file

Read the file

fread()读取文件

Parameter 1: The resource returned by fopen successfully opening the file

Parameter 2: The number of bytes to be read

Return: Return the read content, if it is read to the end Returns the null character.

feof() determines whether the file pointer has reached the end

Parameter 1: The resource returned by fopen successfully opening the file

Return value: If the file Returns true when the pointer reaches the end or an error occurs, returns false when it does not reach the end;

fgets() reads lines from the file pointer

Parameter 1: succeeded by fopen Resources returned by opening the file

Return value: Return the read content

We use code to demonstrate opening the file. First, our function to open the file is fopen(). When we open the file, There is a return value. When we run the result, we find that we are missing the second parameter and the Boolean value reported is: false. That is to say, we can open the file, but we have to tell how to open the file. We are reading. Open or open by writing, so we now open it by reading (the code is as follows). After the code is written, the running result shows that it was successfully opened;

For closing the file: we use fclose() function, if we directly use the fclose function to demonstrate the results, an error will appear, and a parameter will be missing, so we need to specify which parameter to close.

<?php
/**** *打开文件读取文件 关闭文件*********/
$file = fopen(&#39;./a.txt&#39;,&#39;r&#39;);
var_dump($file);
//关闭文件
fclose($file);
?>

When we want to read a file, we need to use the fread() function. After reading, it will return the read content, and then we print out ($content), and we will find that running The result is wrong again, one parameter is still missing

<?php
/*****打开文件读取文件关 闭文件*********/
$file = fopen(&#39; ./a.txt&#39;,&#39;r&#39;);
//var_ dump($file);
//读取文件内容
$content = fread($file,1);
var_ dump($content);
//关闭文件
fclose($file);

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of ​Complete file operations in one day and related operations of reading files (with detailed explanation). 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