Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What are the basics of file handling in PHP?

Behzad Ahmad

Overview

For any application, we need to have file handling. Some tasks require files for the program to work. The file handling in PHP is similar to the file handling in any other programming language. There are many functions to perform the file handling operations in PHP. Those functions are listed below:

  • fopen()

  • fread()

  • fwrite()

  • fclose()

The fopen() function

To use the file, first of all, we have to open the file by using the open() function. The fopen() function accepts two arguments one is the name of the file and the second one is the mode in which we want to open the file.

The different modes to open the file are listed below:

  • r: It only reads the existing file.

  • w: It is used to write a file. If the file doesn’t exist, then it creates the new file and then opens it for writing the file. If the file already exists, then, first, it erases all the contents of the file and then opens it.

  • a: The file is opened for write-only. But the file pointer points to the end of the file, so the existing data is preserved.

  • r+: It is used to read and write the existing file.

  • w+: It is used to read and write a file. If the file doesn’t exist, then it creates the new file and then opens it for writing the file. If the file already exists, then, first, it erases all the contents of the file and then opens it.

  • a+: The file is opened for read and write. But the file pointer points to the end of the file, so the existing data is preserved.

  • x: To create a file with write-only access.

Code example

Let’s look at the example below:

<?php
  $file = fopen("tst.txt",'x');
?>

Code explanation

  • Line 2: We use the fopen() function and pass it the file name and the mode in which we want the file to behave.

The fread() function

After the file is opened, we use the fread() function to read the file. This function takes two arguments:

  • The pointer to the file
  • The size of the file in bytes

Code example

Let’s look at the code below:

main.php
demo.txt
<?php
$name = "demo.txt";
$file = fopen( $name, 'r' );
$size = filesize( $name );
$filedata = fread( $file, $size );
echo $filedata
?>

Code explanation

  • Line 2: We store the file’s name in a variable.

  • Line 3: We open the file in read-only mode.

  • Line 4: We calculate the file size and store it in a variable.

  • Line 5: We call the fread() function and pass the arguments.

  • Line 6: We print the data that we got from the file.

The fwrite() function

The fwrite() function is used to create a file or to append the text to the existing file. This function also takes two arguments:

  • The pointer of the file
  • The message we want to write in the file

Code example

Let’s look at the code below:

main.php
demo.txt
<?php
$file = fopen("test.txt", 'w');
$text = "I am Behzad\n";
fwrite($file, $text);
$file = fopen( "test.txt", 'r' );
$size = filesize( "test.txt" );
$filedata = fread( $file, $size );
echo $filedata
?>

Code explanation

  • Line 2: We open the file.

  • Line 3: We store the text we want to write in the file.

  • Line 4: We call the fwrite() function and pass the arguments to the function.

  • Line 5: We open the file once again but this time in read-only mode.

  • Line 6: We calculate the file size and store it in a variable.

  • Line 7: We call the fread() function and pass the arguments to the function.

  • Line 8: We print the data that we got from the file to see if the file is updated.

The fclose() fucnction

When we use the file then we have to close the file by using the fclose() function.

Code example

Let’s look at the code below:

main.php
demo.txt
<?php
$file = fopen("demo.txt", 'r');
fclose($file);
?>

Code explanation

  • Line 2: We open the file by using the fopen() function.

  • Line 3: We close the file by using the fclose() function.

RELATED TAGS

php
RELATED COURSES

View all Courses

Keep Exploring