Home Backend Development PHP Problem ​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)

Jun 08, 2021 pm 04:58 PM
File operations

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:

1

- is executable()

Copy after login

Whether the file is readable:

1

is readable()

Copy after login

Whether the file is writable:

1

is writable()

Copy after login

Creation time of the employment file:

1

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

Copy after login

Modification time of the employment file:

1

filemtime()

Copy after login

Last access time of the obtained file:

1

- fileatine()

Copy after login

flock simple file lock

Modification time of the obtained file

1

filemtime()

Copy after login

Get the file last access time

1

fileat: ime()

Copy after login

flockSimple file lock

Open file

1

fopen()

Copy after login

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:

1

fclose()

Copy after login

Parameter: the resource returned by fopen successfully opening the file

Read the file

1

fread()读取文件

Copy after login

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.

1

2

3

4

5

6

7

<?php

/**** *打开文件读取文件 关闭文件*********/

$file = fopen('./a.txt','r');

var_dump($file);

//关闭文件

fclose($file);

?>

Copy after login

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

1

2

3

4

5

6

7

8

9

<?php

/*****打开文件读取文件关 闭文件*********/

$file = fopen(' ./a.txt','r');

//var_ dump($file);

//读取文件内容

$content = fread($file,1);

var_ dump($content);

//关闭文件

fclose($file);

Copy after login

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

File operations in PHP8.0: file monitoring File operations in PHP8.0: file monitoring May 14, 2023 pm 02:21 PM

File operations in PHP8.0: file monitoring

Can I delete gho files? Can I delete gho files? Feb 19, 2024 am 11:30 AM

Can I delete gho files?

Go Programming Tips: Deleting Contents from a File Go Programming Tips: Deleting Contents from a File Apr 04, 2024 am 10:06 AM

Go Programming Tips: Deleting Contents from a File

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

How to safely read and write files using Golang?

How to fix: Java file operation error: File write failed How to fix: Java file operation error: File write failed Aug 26, 2023 pm 09:13 PM

How to fix: Java file operation error: File write failed

Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files Jul 29, 2023 pm 10:37 PM

Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files

PHP file operation example: reading CSV file PHP file operation example: reading CSV file Jun 20, 2023 am 11:42 AM

PHP file operation example: reading CSV file

How to insert content at a specified location in a file using C++? How to insert content at a specified location in a file using C++? Jun 04, 2024 pm 03:34 PM

How to insert content at a specified location in a file using C++?

See all articles