fbpx

Request a Call back


captcha

How to Work with Files in PHP? – Part of PHP Training

Generally information on the server is preferred to be stored in a database. But however there may be a situation where a web host might not allow you to have a database. Moreover if the data you are opting for storage is simple and not so important, then storing it in the files on the server is a better option to go with, in order to save up the time, storage space on the server and also efforts of the user in storing and accessing data from database with a specialized query language.

One of the best benefits of storing data in files is the ease of the process. This is a much simpler and easy process to store data in files. Like in the database, here in files you are not required to install any software or create and design the database platform for data storage. All you need to do is to just create the file and store the data with statements in PHP script.

Files are versatile data storage elements which can be accessed or handled through few simple operations and functions in PHP. Let us now consider few examples and have a look at a process through which data can be stored, read and inserted into the files. Let us have a look at a few important PHP file functions that are used to perform various operations on files for data handling.

1.   Opening a file – The fopen()  function as clear from its name enables opening of file. Generally this function takes two parameters – one is the file to be opened and other is the mode in which you want the file to open.

In general there are three modes which can set for the file – read only mode which is specified by ‘r’, write only mode specified by ‘w’ and append mode represented with symbol ‘a’.

In read only mode pointer in the file with name specified is set to beginning of file and is allowed for reading only. The pointer position in write mode too remains the same, which is at the beginning of file. Here the only writing permissions are given and hence if content already exists in file it will be erased and new content will start inserting in the write mode, nevertheless if you want to add new content to the existing content in the file, then choosing to open the file in append mode would be the best option.

However these options are also used in combinations with following notations – ‘r+’ for read and write mode, ‘w+’ for the same read and write and ‘a+’ for append and read mode.

Syntax fopen()

$file_variable = fopen( “filename”, “mode” );

Consider a simple PHP statement for fopen()

<?PHP

{

$file_contents = fopen( “abc.txt”, “r” );//Opening the file abc.txt in the read only mode and storing its file handle or location value in file_contents variable.

print $file_contents;

fclose($file_contents); //close file

}

?>

2.  Reading the File – It’s easy reading files through different ways in PHP. The functions like readfile( ), fgets( ) & file_get_contents() can be used for reading files using PHP.

Syntax for the above functions  

$var_file_contents = readfile( “filename” ); //this opens a file and then reads from it

file_get_contents(file_to_read); //it reads from an already opened file

fgets($file_handle, line_size); // This will read specified number of characters on a single line of text as mentioned in line-size variable

Example for fgets()

<?PHP

$file_var = fopen(“abc.txt”, “r”);

while (!feof($file_var))

{

$text_line_from_file = fgets($file_var); //this will read a line of text from file “abc.txt” which is

already opened

print $text_line_from_file “<BR>”;

}

fclose($file_var); //close file

?>

Example for readfile()

<?PHP

do

{

print readfile(“abc.txt”);//this will open and readfile from beginning till the end of file.

}

while (!feof(“abc.txt”))

{

fclose($file_var); //close file

?>

Example for file_get_contents()

<?PHP

do

{

$file_var = “abc.txt”;

print file_get_contents( $file_var);//just reads from already opened abc.txt file.

}

while (!feof(“abc.txt”))

{

fclose($file_var); //close file

?>

3.  Writing in the File

There are two ways to write to a file by using fwrite() and file_put_contents() methods.

fwrite() function takes two parameters, one being the name of file and other is the content to be written while file_put_contents() will take three of them which include the file name, the data to be written to the file, and the constant representing file mode which can either be write or append.

Syntax fwrite() –

$file_var= fopen(“abc.txt”, “w”);//this opens abc.txt in write mode which means the pointer is set to beginning and already present content in the file is deleted.

fwrite( $file_var, $file_contents );//this write content in “file_contents” variable to the abc.txt file

Syntax file_put_contents() –

file_put_contents($file_handle, $file_contents, context);

This can be written in two ways

file_put_contents($file_handle, $file_contents);// it overwrites the content in  the file. By default the context is set to write mode.

file_put_contents($file_handle, $file_contents, FILE_APPEND);//it appends the content to the file. FILE_APPEND specifies append mode

Example for fwrite()

<?php

$f1 = fopen(“abc.txt”, “a”);

$output = “banana”;

fwrite($f1, $output);

fclose($f1);//close file

?>

Example for file_put_contents()

<?php

$file = “abc.txt”;

file_put_contents($file, “banana”);//this overwrites existing content in abc.txt to banana

file_put_contents($file, “cheese”, FILE_APPEND); //this adds the word “cheese” at the end of the existing content in abc.txt file.

fclose($f1); //close file

?>

Hence, file handling is an important concept in PHP which is taught in detail in the PHP training classes. The professionals at PHP training institute can explain to you these concepts of data storage and access from the files on the server. Tops Technologies is a leading PHP training institute in India where one can learn PHP file handling in detail through real-time practical training. One can join the live project training at Tops to get an opportunity to learn all the PHP concepts practically while working with expert developers on the real-time projects.

Leave a Reply

Your email address will not be published.

Leave a Reply

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

Need Answers

Now???

Our Counsellors can Answer Any Queries.

 Just Request a Call

Thank you

Calling you Soon