PHP File Processing: fopen, fread, fwrite file_exists

PHP File Function

  • To store information on the computer we create a file.
  • This file stores various types of information such as simple text, images, configuration settings of the program, etc.
  • PHP supports various functions to work with these files.
  • This file functions allow you to create the file, open the file, reading contents of the file, writing contents to file, and finally closing the file.
  • In this tutorial, we will learn various file functions.

fopen()

  • In PHP, fopen() function is used to create or open the file.
  • It returns the Boolean value TRUE or FALSE. If the specified file opens successfully then it returns TRUE value otherwise it returns a FALSE value.
  • This function will open the file we have specified if it exists otherwise it will create a new file with the specified name.

Syntax:-

<?php 
	fopen($filename, $filemode, $filepath, $context);
?>

Here,

filename: It specifies the name of the file to be opened.

filemode: It specifies the mode of the file in which you want to open the file.

filepath: Indicates the path to be searched for the specified file. It is an optional argument.

context: Indicates the context of the file to be opened. It is an optional argument.

File mode have one of the following values:

rOpens the file in read-only mode.
r+Opens the file in read/write mode.
wOpens the file in write-only mode. It will delete the previous contents of the file if it exists.
w+Opens the file in read/write mode. It will delete the previous contents of the file if it exists.
aOpens the file in write mode. It will add new content at the end of the file without deleting previous content.
a+Opens the file in read/write mode. It will add new content at the end of the file without deleting previous content.

file_exists()

  • In PHP, this function is used to check whether the file exists or not.
  • We can use this function, to ensure whether the file exists on the server or not while creating the new file.
  • It will return the boolean value true if file exists else returns false

Syntax:-

<?php
	file_exists($filename);
?>

Example:-

<?php
	if(file_exists("config.txt")){
			echo "File Exists";
	}
	else{
			echo "File doesn’t Exists";
	}
?>

Output of above example will be File Exists if file exists in the system else File doesn’t Exists.

fread()

In PHP, fread() function is used to read contents from the specified file.

Syntax:-

fread(File, MaxLength)

Here,

File: It is the name of variable that we use at the time of opening the file.

MaxLength: It is the maximum length in a byte that you can read using this function. It is an required argument

Example:-

<?php
	$file = fopen("config.txt", 'r');
	fread($file, 10);
?>

In the above example, as you can see I have open the file in read mode as I just want to read the file, and it’s necessary to open the file before reading. Also, fread() will return the number of items successfully read.

fwrite()

  • In PHP, fwrite() function is used to write some text into the file.
  • You must open the file before using this function.

Syntax:-

fwrite(File, String, MaxLength)

Here,

File: It is the name of variable that we use at the time of opening the file.

String: It is the text we want to write into the file that is opened.

MaxLength: It is the maximum length in a byte that you can write using this function. It is an optional argument

Example:-

<?php
	$file = fopen("essay.txt", 'w');
	$text = “This is an essay on Environment”;
	fwrite($file, $text);
	fclose($file);
?>

fclose()

  • In PHP, fclose() function is used to close the file.
  • This function returns a Boolean value. If the file is closed successfully it returns true otherwise false.

Syntax:-

fclose($File)

Here,

File: It is the name of variable that we use at the time of opening the file.

Example:-