Tutorial for Closing File in PHP

Closing file in php is all related to fclose( ) Function. With the help of this code we can run closing function in php.

What is fclose( ) Function in php?

In PHP, the fclose() method is used to close a file referenced by an open file pointer. If the method fclose() is successful, it returns true; otherwise, it returns false. It accepts the file that needs to be closed as an input and closes it.


bool fclose( $file )


The $file argument is the only one accepted by PHP’s fclose() function. The file that has to be closed is specified by this argument.

Return Value:

If it succeeds, it returns true; if it fails, it returns false.

Errors And Exception:

  • If you want to read the contents of a file that was written using the fwrite() function, you must first shut it with the fclose() function.
  • For remote files, PHP’s fclose() method does not work. It only works with files that the server’s filesystem can access.

Let’s take examples to explain this function:

Input : $check = fopen("gfg.txt", "r");
Output : true

Input: $check = fopen("singleline.txt", "r");
       $seq = fgets($check);
       while(! feof($check))
         echo $seq ;
         $seq = fgets($check);

Example 1:-


Example 2:- The file test.txt in the software below includes just one line: “This file contains only one line.”


Tagged : / / / / / / / / / / / /

Tutorial for Reading file on File Handling in PHP

What is Reading file on File Handling?

PHP has a number of functions for reading data from files. You may read entire file data, read data line by line, or read data character by character using various methods.

How many types of PHP file Read Functions are:-

The available PHP file read functions are given below.

  • fread()
  • fgets()
  • feof()

PHP readfile() Function

Reads a file and publishes it to the output buffer with the readfile() method.

Assume we have a text file on the server named “webdictionary.txt” that looks like this:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The following is the PHP code for reading the file and writing it to the output buffer (the readfile() method returns the number of bytes read if successful):


If all you want to do is open a file and read its contents, the readfile() method comes in handy.

Tagged : / / / / / / / / / / / /

Tutorial for Opening file on File Handling in PHP

Opening file on File Handling in PHP

PHP File Handling, Create, Open, Read, Write, Append

fopen( ) – This function is used to open a file or URL.


fopen(filename, mode, include_path, context)
File name – It is the name of file or url.
Mode – in which mode we want to open this file ex: – r, w etc.
Include_path – Set this parameter to ‘1’ or TRUE to specify that you want to search for the file in the PHP include path.
Context – Contexts modify or enhance the behavior of the data stream from and to files.

If the open operation fails, it returns FALSE and an error on failure.


Tagged : / / / / /