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:
- open a file
- read the file or write to it
- 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);
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