codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

30 / 56 topics
30File Handling in PHP31File System Functions32Directories in PHP
Tutorials/PHP/File Handling in PHP
🐘PHP

File Handling in PHP

Updated 2026-05-15
10 min read

File Handling in PHP

Introduction

In web development, file handling is a crucial aspect that allows you to interact with the server's file system. This includes reading from and writing to files, which can be used for storing user data, logs, configuration settings, and more. PHP provides several built-in functions to handle files efficiently.

This tutorial will guide you through the basics of file handling in PHP, covering how to read from and write to files using both procedural and object-oriented approaches.

Concept

PHP offers a variety of functions to work with files, including opening, reading, writing, appending, and closing files. The most common functions used for file operations are:

  • fopen(): Opens a file or URL.
  • fclose(): Closes an open file pointer.
  • fread(): Reads from an open file pointer.
  • fwrite(): Writes to an open file pointer.
  • file_get_contents(): Reads the entire file into a string.
  • file_put_contents(): Writes a string to a file.

These functions can be used in both procedural and object-oriented styles. The procedural style is straightforward, while the object-oriented style involves using the SplFileObject class.

Examples

Reading from a File

Procedural Approach

Here's how you can read from a file using the procedural approach:

php
1<?php
2// Open the file for reading
3$file = fopen("example.txt", "r") or die("Unable to open file!");
4
5// Read the entire content of the file
6$content = fread($file, filesize("example.txt"));
7
8// Output the content
9echo $content;
10
11// Close the file
12fclose($file);
13?>

Object-Oriented Approach

Using the SplFileObject class:

php
1<?php
2// Create a new SplFileObject instance for reading
3$file = new SplFileObject("example.txt");
4
5// Read and output each line of the file
6while (!$file->eof()) {
7 echo $file->fgets();
8}
9?>

Writing to a File

Procedural Approach

Here's how you can write to a file using the procedural approach:

php
1<?php
2// Open the file for writing
3$file = fopen("example.txt", "w") or die("Unable to open file!");
4
5// Write some data to the file
6fwrite($file, "Hello, World!");
7
8// Close the file
9fclose($file);
10?>

Object-Oriented Approach

Using the SplFileObject class:

php
1<?php
2// Create a new SplFileObject instance for writing
3$file = new SplFileObject("example.txt", "w");
4
5// Write some data to the file
6$file->fwrite("Hello, World!");
7?>

Appending to a File

Procedural Approach

Here's how you can append to a file using the procedural approach:

php
1<?php
2// Open the file for appending
3$file = fopen("example.txt", "a") or die("Unable to open file!");
4
5// Append some data to the file
6fwrite($file, "
7Appending new line.");
8
9// Close the file
10fclose($file);
11?>

Object-Oriented Approach

Using the SplFileObject class:

php
1<?php
2// Create a new SplFileObject instance for appending
3$file = new SplFileObject("example.txt", "a");
4
5// Append some data to the file
6$file->fwrite("
7Appending new line.");
8?>

Using file_get_contents() and file_put_contents()

These functions provide a simpler way to read from and write to files:

Reading with file_get_contents()

php
1<?php
2// Read the entire content of the file into a string
3$content = file_get_contents("example.txt");
4
5// Output the content
6echo $content;
7?>

Writing with file_put_contents()

php
1<?php
2// Write a string to a file
3file_put_contents("example.txt", "Hello, World!");
4?>

What's Next?

In the next section, we will explore more advanced file system functions in PHP, such as directory handling, moving files, deleting files, and changing file permissions. These functions will further enhance your ability to manage files on a server.

Stay tuned for more tutorials on PHP and web development!


PreviousTimezones in PHPNext File System Functions

Recommended Gear

Timezones in PHPFile System Functions