---
title: "File Handling in PHP"  
description: "fopen() function is used to open a file, but in PHP same function is used to create a file. If you use fopen() on a file that does not exists, it will"  
author: "zack mathews"  
published: 2016-06-07  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11144/file-handling-in-php  
category: "php"  
tags: ["php", "file", "handler"]  
reading_time: 3 minutes  

---

# File Handling in PHP

##### This chapter include following functions related to:

- Opening the files.\
- Reading a file\
- Writing a file\
- Closing a file

##### Opening and closing files:

**fopen()** function is used to open a file, but in PHP same function is used to create a file. If you use fopen() on a file that does not exists, it will create that file. It has two parameter :

- Name of the file
- Mode of the file

**There are six types of mode:**

| ## Mode | ## Purpose |
| --- | --- |
| ## r | Open a file for read only. File pointer will be at the start of the file. |
| ## w | Open a file for write only. If file does not exist than it will create the file The file pointer is at the beginning of the file. |
| ## a | Open a file for write only. If it does not exist than it will create the file. The file pointer is at the end of the file. |
| ## r+ | Open a file for read/write. File pointer starts at the beginning of the file. |
| ## w+ | Open a file for read/write. Creates a new [file if](https://www.mindstick.com/forum/161599/how-do-you-create-a-file-if-it-doesn-t-exist-and-then-append-to-it) it does not exist. File pointer starts at the beginning of the file. |
| ## a+ | Open a file read/write. The existing data is preserved. Creates a new file if it does not exist. The file pointer is at the end of the file. |

**NOTE:**After making the changes to the opened file the file must be close. It is mandatory to close the file. To close the file we use fclose() function.

##### Reading a File:

Once we open a file (using fopen()) it can be read by fread() function. This function has two parameter :

File pointer and the size of the file expressed in bytes.

**Note:**The length of the file can be determined by the **filesize().**

Following are the steps to read a file:

**Step1:** Open a file using fopen().

**Step2:** Get the [file size](https://www.mindstick.com/forum/12663/how-to-set-file-size-in-grid-view) using filesize().

**Step 3:** Read the file using fread().

**Step 4:** Close the file using fclose().

##### Create intro.txt file:

SQL = [Structured Query Language](https://www.mindstick.com/blog/11168/introduction-to-sql)</br> HTML = Hyper Text [Markup Language](https://www.mindstick.com/interview/234/what-is-markup-language)</br> AJAX = [Asynchronous](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) and XML</br> CSS = [Cascading](https://www.mindstick.com/blog/94/css-cascading-stylesheet) [Style Sheets](https://www.mindstick.com/forum/506/why-do-we-use-style-sheets)</br> PHP = PHP Hypertext Preprocessor

```
<?php
// fopen() will open a file intro.txt
$myfile = fopen("intro.txt", "r") or die("Unable to open file!");// if there is no file intro.txt it will give a message unable to open a file.
//’r’ is the read mode.
echo fread($myfile,filesize("intro.txt"));//filesize() will get the size of the file.
fclose($myfile);//fclose() will closed the file
?> 
```

**Output:** SQL = Structured Query Language HTML = Hyper Text Markup Language AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets PHP = PHP Hypertext Preprocessor

##### \

##### fwrite():

A new file can be created and text could be written using **fwrite().**This function has two parameter a pointer and a string to write .

```
<?php$file = fopen("php.txt", "w") or die("Unable to open file!");$text = "Neha singh\n";fwrite($file, $text);$text = "Rakesh singh\n";fwrite($file, $text); //write the text on file.fclose($file);?>
```

**Output**

Neha singh

Rakesh singh

\

PHP overwriting

Now, “php.txt” contains some data. Now when we open this file it will erase the existing data.

```
<?php$file = fopen("php.txt", "w") or die("Unable to open file!");$text = "Arun garg\n";fwrite($file, $text);$text = "Raghu tandon \n";fwrite($file, $text);fclose($file);?>
```

**Output:**

Arun garg

Raghu tandon

\

---

Original Source: https://www.mindstick.com/blog/11144/file-handling-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
