File Handling in PHP:

In PHP you can manipulate files like you can create a file, open a file, edit a file etc. In PHP you can interact with files easily.

When you are performing some action on files be very careful because some times you are editing a file which is wrong file etc.

In This topic we will learn how to create a file, open a file , close a file and also we can perform some tasks over a file like read, write, append etc.

We will perform following task on a File:

  • Create a File
  • Open a File
  • Close a File
  • Read a File
  • Write a File
  • Append a File

 Create a file in PHP:

To create a file in PHP “fopen()” function is used. One thing you have to notice that “fopen()” function is also use to open a file. It is little confusing but the purpose of “fopen()” function is if the file is exist than it will open the file and if it is not exist than it will create a new file.

Example:

When you create a file than it will save in our htdoc folder because we executing it on localhost

 Write on a file in PHP:

The “fwrite()” function is used to write in a file. You can write in a file through PHP coding.

Example:

We already created a file name “myFile.txt” now we will write something on that file it

 Open a file in PHP:

The “fopen(()” function is used to open a file. You can open in a file through PHP coding. If the file is exists than it will open and if it is not exists than it will first creates the file.

If you are opening a file always open it in read mode if you open it in write mode it will not open because it will delete all the content of the existing file.

Example:

 

There is many modes to open a file in PH. They are as follows:

Mode Description
r Open the file in read only mode and the cursor is in the beginning
w Open the file to write the content and if there is some content already in that file than it first erase the content and than start writing
a Open the file for writing only and you can write the data after the existing data it will not erase data like happen in write mode
x Create a new file and only write data in file and if the file exists than throw the error
r+ Open the file in read/write mode and the cursor is in the beginning
w+ If file does not exists than creates a new file and if file exists than erase the data and than start writing
a+ If file does not exists than creates a new file and if file exists than add data to the existing file
x+ If the file is exists than throw the error and if not that creates new file for reading and writing

 Close a file in PHP:

To close a opened file in PHP use “fclose()” function.

Example:

 Read a file in PHP:

If you want to only read a file for that in PHP “fread()” function is used.

In “fread()” function you have to pass two arguments the first argument is the is the file name ad the second argument is the size of the file.

One thing you have to notice that the file should not be empty otherwise it will throw an error.

 Example:

 Append a File in PHP:

If you want to write more text in the existing file at this time we open a file in a “append” format and add some data to the file.

Example:

 

Leave a Reply

Your email address will not be published. Required fields are marked *