logo
logo

Get in touch

Awesome Image Awesome Image

Development May 19, 2014

How to open, read and write a file using php [ Tutorial – PHP ]

Writen by Taeyaar Support

comments 0

It is one of the most powerful features of PHP, where you can write, read the content of a file. Basically, we generally use this to make logs of our website, but also can be used for any other purpose. So, it’s very easy to do so. First, I’ll explain syntax then an example and after that a brief explanation. So let’s get started!

Opening a file

To read a file, fread () command is used in PHP, where fread () reads the opened file using fopen (). The syntax for fopen is:

?

1fopen(“<span id=”IL_AD11″>Filename</span>”,mode)

Where filename contains your file’s exact name with extension and file path if it isn’t in the same directory where mode describes the permission of the file, getting opened. Various modes for fopen() command are described below:

  • r  [ Opening for read only ]
  • r+  [ Opening for reading and writing ]
  • w [ Opening for writing only and if the file doesn’t exist, it attempts to create one. ]
  • w+ [ Opening for writing and reading, also attempts to create a new one if doesn’t exist. ]

Now let me show an example of opening a file using fread() in php. Suppose, I’ve a file in the root directory with name conf.txt and I’m going to open with read only mode.

?

123<?php$handle = fopen(“conf.txt”,”r”);?>

Reading from a file:

In the above example, we’ve successfully opened the file and assigned it to handle variable. Now it’s time to read the content of file using fread. Basic syntax of fread() command is:

?

1fread(resource,int length);

Where resource is our handle variable, in which we’ve opened the file conf.txt with read only permission. Now lets see an example how to read content from file and display it as output on web browser using php.

?

<?php$handle = fopen(“conf.txt”, “r”);$read = fread($handle, filesize($filename));

fclose($handle);

?>

You are recommended to close the file after getting its content as it saves the resources. Once you have assigned the content in reading variable, simply close the file your data will stay in variable. Now you’ve successfully read the data from file conf.txt.

Writing to the file

After successful in reading and opening a file using PHP, it isn’t hard to write a file. Very simple! We do use fwrite() command in php to write strings to a file using php! First set the mode to w, so we can write the file.

?

<?php$handle = fopen(‘config.txt’, ‘w’);fwrite($handle, ’420′);

fclose($handle);

?>

In the above example, we’ve opened it using write mod(w), after that fwrite(resource, string) where string is the text to be written in the file. Text will be written next the text in a file. If you want to make a new line you can use n to start from a new line in the file. For example:

?

<?php$handle = fopen(‘config.txt’, ‘w’);fwrite($handle, ’420′);

fwrite($handle, ‘n840n1260′);

fclose($handle);

?>

In this example, we wrote the string 420, 840 in next line and 1260 in next line. I think these examples are enough to understand how fread, fwrite and fopen works. If you have any problem, feel free to reply!

Tags : ,