Free cookie consent management tool by TermsFeed Generator PHP Read and Write to Files | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Read and Write to Files

PHP Read and Write to Files

Last Updated on Mar 21, 2023

Process of working with files

There are many functions that can help you read the contents of a file. we are going through the most common functions.

The process of working with files is:

  1. open a file
  2. read the file or write to it
  3. close the file

So let’s go through them step by step

1. Open the file

You can open a file with fopen() First argument is the path to the file and it’s name like: “./test.txt”

And the second argument is a flag to tell PHP what do you want to do with the file

The common flags are:

  • r: want to read the file
  • w: want to write to the file
  • a: want to append the file

We want to tell php to open the file in the current directory with the name example.txt and open it for reading only. so we write:

$file = fopen('./example.txt','r');

We are telling php to open the file in the current directory with the name example.txt and open it for reading only.

2. Read the file or write to it

2A.Reading a file

Let’s start by reading a file. There are many functions to help us read a file but the best approach is to read it line by line. That way we wouldn't face any memory problems.

In order to read a line we should use fgets() and it gets a file as the argument

$line = fgets($file);

Everytime you run fgets() it will read the next line

$first_line = fgets($file);
$second_line = fgets($file);

It will take care of keeping track of which line it read last. And if there is no more line to read it will return false.

We are going to use this knowledge and write a while loop to go through every line of the file. So let’s do it.

$line = fgets($file);
while ($line != false) {
    echo $line;
    $line = fgets($file);
}

2B.Writing to a file.

Now let’s talk about writing to a file. First make sure that when you open the file you’d give it flag "w" or "a" so PHP would open the file for writing and not reading. then you can write to it with fwrite();

The first argument is the file and the second argument is the content you want to write.

fwrite($file,'Hello World!');

You can write all your content at once or you can call fwrite multiple times.

It doesn’t matter if you call fwrite once or many times, it doesn’t create new lines in the file.

if you want to have a new line then you should specify the new line character yourself.

fwrite($file,'Hello World!');
fwrite($file,'Hello World!');

// output is
// Hello World!Hello World!

if you want new lines you should add new line character which is \n and make sure you use double quotes

fwrite($file,"Hello World!\n");
fwrite($file,"Hello World!\n");

// output is
// Hello World!
// Hello World!

Or you could add PHP_EOL to the end of your string. It’s a constant in php and it will add end of line (EOL) character for you

fwrite($file,'Hello World!'.PHP_EOL);
fwrite($file,'Hello World!'.PHP_EOL);

// output is
// Hello World!
// Hello World!

3. Close the file

Now it’s the last step and it’s very important to always close the file. It’s very easy just run fclose() It gets the file as the argument so in our case, to close the file I had open I can run:

fclose($file);

Put it All Together

and here is everything we learned about working with files:

<?php
// writing to a file
$file = fopen('./example.txt','w');
fwrite($file,'Hello World!'.PHP_EOL);
fwrite($file,'Hello Pratham!'.PHP_EOL);
fclose($file);

// appending to a file
$file = fopen('./example.txt','a');
fwrite($file,'Hello World!'.PHP_EOL);
fwrite($file,'Hello Pratham!'.PHP_EOL);
fclose($file);

// reading a file
$file = fopen('./example.txt','r');
$line = fgets($file);
while($line != false){
    echo $line;
    $line = fgets($file);
}
fclose($file);

https://youtu.be/jg6Yt8WSSMI

Conclusion

Now you know about working with files in PHP.

I recommend you to open a PHP file and try to create a new text file, write contents to it and then try to read that content. close it and try to open it again and append content to it.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • working with files in PHP
  • reading a file
  • writing to a file
  • flags for working with a file
  • open and close a file

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP session
Mar 21, 2023 programming

PHP session

Today we are going to learn about session in PHP. Sessions are like cookies but instead of storing the key value pairs on the user’s computer, session stores them on the server. ...

8 Min Read Read More
PHP class
Mar 22, 2023 programming

PHP class

Today we are going to talk about classes in PHP. This is an introduction to a series of posts that would cover the topic of object oriented programming in PHP. class is the first one. ...

8 Min Read Read More
PHP Simple Tinker Like Script
Feb 15, 2023 programming

PHP Simple Tinker Like Script

Today we are going to build a simple Tinker in PHP. Tinker is a tool that allows users to interact with the application through the command line. ...

8 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...