File Handling PHP

File Handling or File System in PHP

Posted by

You work as a web developer, you need to know the file system or file handling in PHP. The file is used to store the information and it can be Configuration, Log, images, Videos, CSV, etc. PHP has multiple functions for handling files.

In this article, we will learn the following topics:

Create or Open a file in PHP

We use fopen() function to create or open a file in PHP.

Syntax:

Filename: It will be the name of the file which you have to create.

Mode: Mode can be “w” or “a”. All modes are described below.

Example:

In the above example, the file “test.txt” has been created and opened in write mode.

Close a file in PHP:

We use fclose() function to close a file in PHP.

Syntax:

Handle: It will be the file that is open to write or read or append.

Write a file in PHP

We use fwrite() function to write a file in PHP. First, we will open the file in writing mode and write the content in that file.

Syntax:

Handle: It will be the file which we will write.

String: It will be the content to be written.

Example:

Read a file in PHP

The first parameter of fopen() function will be the file name and the second parameter will be in reading mode (“r”) and we use fread() function to read a file in PHP.

Syntax:

Handle: It will be the file which we will read.

Length: We use filesize() function to calculate the length of the file.

Example:

Append a file in PHP

We open a file in append mode(“a”) to appending a file in PHP. The file does not exist then it creates a new one.

Example:

Mode description in PHP

Mode Description
r It is used to read the file only. Begins at the beginning of the file
r+ It is used to open a file in reading and writing mode. Begins at the start of the file
w It is used to open a file in write mode only. Opens and clears the file contents; or creates a new file when it doesn’t exist
w+ It is used to open a file in reading and writing mode. Opens and clears the file contents. It creates a new file when it doesn’t exist
a It is used to open a file in append mode. Opens and writes to the end of the file, or creates a new file if it is not available
a+ You can read and append file using this mode. It sets the file pointer at the file end. If the file does not exist then it creates.

Check file existence in PHP

We use file_exists() function to check file existence in PHP.

Syntax:

Deleting a file in PHP

The unlink() function is used to delete a file in PHP.

Syntax:

Example:

In the above example, the file will be deleted if it will exist. Suppose the file does not exist then it will show a warning error message. In this case, you can check the file by condition statement.

Example:

Determining file size in PHP

The filesize() function is used to determine file size in PHP. It returns file size in bytes.

Syntax:

Example:

Rename file in PHP

We use rename() function to rename a file in PHP.

Syntax:

Example:

Copy file in PHP

We use copy() function to copy a file in PHP from one destination to another.

Syntax:

Example:

Leave a Reply

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