0% found this document useful (0 votes)
31 views

PHP Tizag Tutorial-59

The document discusses closing files in PHP after opening and using them. It notes that while PHP will automatically close files after code execution, it is still good practice for programmers to manually close files using fclose to avoid potential errors and free up resources. As an example, it shows how to open a file, write to it, and then close it using fclose and passing the file handle.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

PHP Tizag Tutorial-59

The document discusses closing files in PHP after opening and using them. It notes that while PHP will automatically close files after code execution, it is still good practice for programmers to manually close files using fclose to avoid potential errors and free up resources. As an example, it shows how to open a file, write to it, and then close it using fclose and passing the file handle.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - File Close

The next logical step after you have opened a file and finished your business with it is to close that file
down. You don't want an open file running around on your server taking up resources and causing mischief!

PHP - File Close Description

In PHP it is not system critical to close all your files after using them because the server will close all files
after the PHP code finishes execution. However the programmer is still free to make mistakes (i.e. editing a file
that you accidentally forgot to close). You should close all files after you have finished with them because it's a
good programming practice and because we told you to!

PHP - File Close Function

In a previous tutorial, we had a call to the function fclose to close down a file after we were done with it.
Here we will repeat that example and discuss the importance of closing a file.

PHP Code:
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

The function fclose requires the file handle that we want to close down. In our example we set our variable
"$fileHandle" equal to the file handle returned by the fopen function.
After a file has been closed down with fclose it is impossible to read, write or append to that file unless it is
once more opened up with the fopen function.

You might also like