Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to open and close files with php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to open and close files with php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In php, you can use the fopen () function to open a file with the syntax "fopen (filename,mode,path,context)"; you can use the fclose () function to close a file, which closes an open file with the syntax "fclose (file)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php opens a file

You can use the fopen () function in PHP to open a file or URL. If the opening is successful, the file pointer resource is returned; if the opening fails, FALSE is returned. The syntax format of this function is as follows:

Fopen (filename,mode,path,context)

The parameters are described as follows:

$filename: for the URL of the file to be opened, the URL can be the absolute path in the server where the file resides, the relative path or the file in the network resource

$mode: used to set how the file is opened (file mode). For specific values, please refer to the introduction in "reading files character by character in php file operations".

Path: optional. If you also need to search for files in include_path, you can set path to 1 or TRUE.

$context: optional parameter, added support for Context in PHP5.0.0.

Example: use the fopen () function to open a file

The running results are as follows:

Resource (3) of type (stream) resource (4) of type (stream) resource (5) of type (stream)

Php closes files

The resource type is one of the basic types of PHP. Once you have finished processing the resource, be sure to turn it off, otherwise some unexpected errors may occur.

The function fclose () closes an open file and returns TRUE on success and FALSE on failure. The syntax format of the function is as follows:

Fclose ($file)

Where $file is the pointer to the file to be closed, which must be valid and opened successfully through the fopen () or fsockopen () function.

[example] use fclose () to close the file pointer.

The running results are as follows:

Before the file pointer is closed: resource (3) of type (stream) after the file pointer is closed: resource (3) of type (Unknown) "how to open and close files with php" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report