Home >Backend Development >PHP Problem >How to open and close files using php

How to open and close files using php

青灯夜游
青灯夜游Original
2021-10-29 18:58:363099browse

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

How to open and close files using php

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

php opens the 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 parameter description is as follows:

  • $ filename: is the URL of the file to be opened. This URL can be the absolute path in the server where the file is located, or it can be a relative path or a file in a network resource;

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

  • path: Optional parameter, if you also need to search for files in include_path, you can set path to 1 or TRUE;

  • $ context: Optional parameter, support for context (Context) was added in PHP5.0.0.

Example: Use the fopen() function to open the file

<?php
    $handle = fopen("./error/400.html", "r");
    var_dump($handle);echo &#39;<br>&#39;;
    $handle = fopen("D:/install/phpstudy/WWW/index.html", "wb");
    var_dump($handle);echo &#39;<br>&#39;;
    $handle = fopen("http://c.biancheng.net/", "r");
    var_dump($handle);
?>

The running results are as follows:

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

php close the file

The resource type is one of the basic types of PHP. Once the resource processing is completed, it must be closed, otherwise some unexpected errors may occur.

The function fclose() can close an open file, returning TRUE if successful, and FALSE if failed. The syntax format of the function is as follows:

fclose($file)

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

[Example] Use fclose() to close the file pointer.

<?php
    header("Content-type:text/html;charset=utf-8");
    $handle = fopen("http://c.biancheng.net/", "r");
    echo &#39;文件指针关闭之前:&#39;;
    var_dump($handle);
    fclose($handle);
    echo &#39;<br>文件指针关闭之后:&#39;;
    var_dump($handle);
?>

The running results are as follows:

文件指针关闭之前:resource(3) of type (stream)
文件指针关闭之后:resource(3) of type (Unknown)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to open and close files using 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