Home > DeveloperSection > Blogs > File Handling in PHP

File Handling in PHP


PHP PHP  File  Handler 
Ratings:
0 Comment(s)
 519  View(s)
Rate this:

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 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 using filesize().

Step 3: Read the file using fread().

Step 4: Close the file using fclose().

Create intro.txt file:

SQL = Structured Query Language</br>

HTML = Hyper Text Markup Language</br>

AJAX = Asynchronous JavaScript and XML</br>

CSS = Cascading 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);

?>

 

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

 


Don't want to miss updates? Please click the below button!

Follow MindStick