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

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

File System Functions

Updated 2026-05-15
10 min read

File System Functions

Introduction

In PHP, handling files is a common task that involves reading from and writing to files. PHP provides a variety of built-in functions to perform these operations efficiently. This tutorial will cover the essential file system functions in PHP, including how to open, read, write, and close files.

Concept

PHP offers several functions for file operations. Here are some fundamental ones:

  • 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 entire file into a string.
  • file_put_contents(): Writes a string to a file.

These functions allow you to perform basic file operations, making it easier to manage files on the server.

Examples

Opening and Closing Files

To work with a file, you first need to open it using fopen(). The function takes two parameters: the filename and the mode in which to open the file. Common modes include:

  • 'r': Open for reading only; place the file pointer at the beginning of the file.
  • 'w': Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
  • 'a': Open for writing only; place the file pointer at the end of the file if the file exists. If the file does not exist, attempt to create it.

Here's how you can open a file and then close it:

php
1<?php
2$file = fopen("example.txt", "w");
3if ($file) {
4 echo "File opened successfully.";
5} else {
6 echo "Failed to open file.";
7}
8fclose($file);
9echo "File closed.";
10?>

Reading from a File

To read from a file, you can use fread() or file_get_contents(). The fread() function reads a specified number of bytes from an open file pointer.

Here's an example using fread():

php
1<?php
2$file = fopen("example.txt", "r");
3if ($file) {
4 $content = fread($file, filesize("example.txt"));
5 echo $content;
6 fclose($file);
7} else {
8 echo "Failed to open file.";
9}
10?>

Alternatively, you can use file_get_contents() to read the entire content of a file into a string:

php
1<?php
2$content = file_get_contents("example.txt");
3if ($content !== false) {
4 echo $content;
5} else {
6 echo "Failed to read file.";
7}
8?>

Writing to a File

To write data to a file, you can use fwrite() or file_put_contents(). The fwrite() function writes a string to an open file pointer.

Here's an example using fwrite():

php
1<?php
2$file = fopen("example.txt", "w");
3if ($file) {
4 $data = "Hello, world!";
5 fwrite($file, $data);
6 fclose($file);
7 echo "Data written successfully.";
8} else {
9 echo "Failed to open file.";
10}
11?>

Alternatively, you can use file_put_contents() to write a string to a file:

php
1<?php
2$data = "Hello, world!";
3$result = file_put_contents("example.txt", $data);
4if ($result !== false) {
5 echo "Data written successfully.";
6} else {
7 echo "Failed to write data.";
8}
9?>

Checking File Existence

Before performing operations on a file, it's often useful to check if the file exists. You can use file_exists() for this purpose.

php
1<?php
2if (file_exists("example.txt")) {
3 echo "File exists.";
4} else {
5 echo "File does not exist.";
6}
7?>

What's Next?

Now that you have a good understanding of file system functions in PHP, the next step is to explore how to handle directories. Directories are collections of files and subdirectories, and PHP provides several functions to manage them.

Stay tuned for more tutorials on advanced file handling and directory management in PHP!


PreviousFile Handling in PHPNext Directories in PHP

Recommended Gear

File Handling in PHPDirectories in PHP