File Read/Write

Introduction

File handling is an essential part of programming that allows applications to create, read, update, and delete files. File Read/Write operations help developers store data permanently and manage information efficiently.

In PHP, file handling functions make it easy to work with text files, logs, reports, configuration files, and user-generated content.

Objectives

By the end of this training, you will be able to:

  • Understand file handling concepts
  • Create and open files in PHP
  • Read data from files
  • Write and append data to files
  • Close files properly
  • Check file existence and permissions
  • Handle file operations safely
  • Build simple file management applications

What is File Read/Write

File Read/Write refers to the process of accessing file content for reading information or storing data into files.

File Reading

Used to retrieve data stored inside a file.

File Writing

Used to create new content or update existing file content.

Importance of File Handling

File handling is important because it helps developers:

  • Store data permanently
  • Generate reports and logs
  • Save user information
  • Manage application settings
  • Process uploaded files
  • Build data-driven applications

Common PHP File Functions

fopen()

Used to open a file.

<?php
$file = fopen("data.txt", "r");
?>

fclose()

Used to close a file after use.

<?php
fclose($file);
?>

fread()

Reads data from a file.

<?php
$file = fopen("data.txt", "r");
echo fread($file, filesize("data.txt"));
fclose($file);
?>

fwrite()

Writes data into a file.

<?php
$file = fopen("data.txt", "w");
fwrite($file, "Welcome to PHP File Handling");
fclose($file);
?>

File Modes in PHP

Read Mode

r

Opens a file for reading only.

Write Mode

w

Opens a file for writing and clears existing content.

Append Mode

a

Adds new content at the end of the file.

Read and Write Mode

r+

Allows both reading and writing.

Reading Files Line by Line

<?php
$file = fopen("data.txt", "r");

while(!feof($file)) {
echo fgets($file);
}

fclose($file);
?>

Appending Data to a File

<?php
$file = fopen("data.txt", "a");
fwrite($file, "\nNew Record Added");
fclose($file);
?>

Checking if a File Exists

<?php
if(file_exists("data.txt")) {
echo "File Exists";
} else {
echo "File Not Found";
}
?>

Creating a New File

<?php
$file = fopen("newfile.txt", "w");
fclose($file);
?>

Deleting a File

<?php
unlink("oldfile.txt");
?>

File Handling Best Practices

  • Always close files after use
  • Check if files exist before reading
  • Use proper file permissions
  • Validate user input for file operations
  • Avoid overwriting important files
  • Handle errors properly

Advantages of File Read/Write

  • Permanent data storage
  • Easy report generation
  • Improved application management
  • Efficient log handling
  • Better data organization

Real World Applications

File handling is used in:

  • Student management systems
  • Attendance systems
  • Website logs
  • Report generation tools
  • Content management systems
  • Backup systems

Practical Exercise

Create a PHP application that:

  • Creates a text file
  • Stores student records
  • Reads and displays stored data
  • Appends new records
  • Deletes unwanted files

Final Presentation

In your final presentation, explain:

  • What File Read/Write means
  • Importance of file handling
  • Common PHP file functions
  • File modes and their uses
  • Reading and writing operations
  • Real-world applications of file handling
Home Ā» Intermediate PHP > Strings and Files > File Read/Write