Home > Backend Development > PHP Tutorial > How to close an open file in php

How to close an open file in php

WBOY
Release: 2016-07-25 08:56:59
Original
1105 people have browsed it
This article introduces how to close an open file in PHP programming. Friends in need can refer to it.

In PHP, when a file is opened for related operations, the file handle needs to be closed in time to avoid long-term resource occupation.

php close file

In PHP programming, when you finish using a file, you must close it in time. This is a good habit.

php function or method to close a file

To close a file in php, you need to use the function fclose.

The following is an example of closing a file for reference.

Code:

<?php
//关闭文件
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
//by bbs.it-home.org
?>
Copy after login

Code description: The fclose function requires a file handle to be closed. In the above example, we set the variable "$ourFileHandle" to the file handle returned by the fopen function.

After performing the above operation, the opened file testfile.txt will be closed. Any subsequent operations such as reading or appending the file content will be invalid. The above operation can only continue when the access file is opened again.

This is about how to close files in php. I hope it will be helpful to everyone.



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