Skip to content

How To Create A Text File In Php?

In this article you are going to learn how to create a text file in php. Basically, there are two ways to create a text file in php. The first one is the use of function fopen() which can be used in PHP 4, PHP 5 and PHP 7 versions. The second method to create a text file is using the function file_put_contents() which can be used in PHP 5 and PHP 7 versions.

The function file_put_contents() is primarily used to write data to a text file. There are two conditions :

  • If the text file exists, it will overwrite the data into the file(erase the previous data). To prevent the text file to be overwritten, you must set the FILE APPEND flag.
  • But, if the text file does not exist, it will create the file.

The same thing goes with function fopen(). It is primarily used to open a text file or URL. Although, it can also be used to create a text file depending on the following two conditions :

  • If the text file already exists, it will open the file and overwrite the current data into the existing data(erase the existing data).
  • But, if the file does not exist, it will create the file by using a mode parameter such as ‘w’, ‘a’, ‘r’ etc.

Both the above function can be used to create a text file in php. And now we are going to describe file_put_contents() and fopen() functions with examples of each. We are one of best php training Institute in India, you may also join our php training program to improve your php development skills.

CREATING A TEXT FILE IN PHP USING FOPEN() FUNCTION

Syntax : fopen($filename, $mode, $use_include_path, $context)

Four types of parameters are used in this function namely $file, $mode, $use_include_path and $context as shown in the syntax. Meaning of these parameters are shown below.

  1. $filename –  name of the file or the path where the data is to be written. For instance, $filename=”newfile.txt”
  2. $mode – this parameter denotes the type of access you want. Some of the mode parameters used in fopen() function are ‘w’, ‘r’, ‘a’, ‘x’, ‘c’, ‘e’.
  3. $use_include_path – this is an additional parameter used to search the text file in the directory.
  4. $context – specifies a stream context resource.

After creating a text file using fopen() function, you can write data to the file by using fwrite() function. Functions like fread() and fclose() can also be used to read and close the text file respectively. Syntax of each function is shown below.

fwrite($handle, $data, $length)
fread($handle, $length)
fclose($handle)

The parameters used in above functions are defined below.

  1. $handle – it is a special variable which is created by using the function fopen().
  2. $data – it is the data to be written. It may be a string, a stream resource or an array.
  3. $length – it denotes the length of the argument.
php-file1-1024x514

Example 1:

php-file1output-1024x518

Output :

php-file2-1024x524

Example 2 :

php-file2output-1024x524

Output :

CREATING A TEXT FILE IN PHP USING FILE_PUT_CONTENTS() FUNCTION

Syntax : file_put_contents($filename, $data, $flags, $context)

Four types of parameters are used in this function $file, $data, $flags and $context as shown in the syntax. Meaning of these parameters are shown below.

  1. $filename – specifies the name of the file or the path where the data is to be written.
  2. $data – specifies the data to be written. It may be a string, a stream resource or an array.
  3. $flags – these are additional parameters separated by the OR(|) operator. Some of the flags used in file_put_contents() function are FILE_APPEND(prevents overwriting the data into existing file), FILE_USE_INCLUDE_PATH(search the text file in the directory), LOCK_EX(locks the current text file).
  4. $context – specifies a stream context resource.
php-file-content-1024x516

Example :

php-file-contentoutput-1024x346

Output :

We have described above the two ways to create a text file using php. Hope this article helped you to eliminate all your confusions. For further updates, stay tuned to our php blogs.

Facebook
Twitter
LinkedIn
Pinterest