Php-training

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

Author Image Icon

Niral Modi

Last Updated: 17 Nov 2023


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.
Placement Banner

Get 100% Job Assistance & get placed in your dream company

Job Assistance
3000+ Companies Tie-Ups

Enter Your Details Now
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()


{

$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()


$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 “
”;

}

fclose($file_var); //close file

?>

Example for readfile()


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()


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()


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

$output = “banana”;

fwrite($f1, $output);

fclose($f1);//close file

?>

Example for file_put_contents()


$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

?>

This is a PHP tutorial full course that covers the basics of programming.

What you will learn:

- The basics of programming in PHP

- How to create your own website using PHP

- How to use MySQL database in PHP applications

The TOPS Technologies course is designed to teach students about the latest and greatest technologies. It will cover topics like web development, cyber security, programming languages, and more. If you want to be successful in the IT field, it's important that you have a good understanding of all the latest technologies. The training course is perfect for anyone who needs to be ready for a career in the IT field. Learn android programming from our expert trainers and gain skills that are in high demand! The training course is perfect for anyone who needs to be ready for a career in the IT field. Learn php courses from our php training institute. 

We provide the best PHP course in surat, Vadodara, Ahmedabad, Rajkot, and Nagpur with live projects and 100% placement assistance.

Author Bio: 

Niral Modi works as a Chief Executive Officer at TOPS Technologies Pvt, which is an Education company with an estimated 303 employees; and was founded in 2008. They are part of the Executive team within the C-Suite Department, and their management level is C-Level. Niral is currently based in Chicago, United States.

TOPS Technologies is the Most Trusted Training Institute, offering Software Training, Hardware Training Classes, Graphic Designing & Web Design Training through Live Project Training. Having tie-ups with 3000+ IT software development companies, we provide a 100% Job Guarantee in Software Development Courses. We are known for our training courses in Python, Java, Android & iOS, AspdotNet, C & C++, Angular Courses, IoT, Software Testing, CCNA, Cloud Computing, Ethical Hacking, Hardware Networking, Cyber Security Training, Digital Marketing, and SEO. We also teach Laravel, Nodejs, Unity 3D Game Development, Machine Learning with Python, Data Science, and Linux server training!"

You can Check out our YouTube channel for more information on any course, such as PHP course, graphic design, and web design, among others.


Stay Connected