Many times the there is the needing to save the data.
There are two ways to do it:
- Saving the data in a file (.txt, .dat, ecc...)
- Saving the data in a database (Access, Mysql, ecc...)
Now we analyze the first case, saving the data in a file.
It is more convenient to save data in a text file when you
have to save a little quantity of data, on the contrary
when you have to save, and therefore interact with a great
quantity of data it is better to use a database. In this
article we want to see how to read and write in a file named
"pippo.txt".
Writing on files
Before writing on a file we must open it, therefore we
use the following expression:
$write_file=fopen("pippo.txt","w");
$write_file is called pointer and it is used to open the
file by the istruction fopen that has, as first argument,
the name of the file and, as second argument, the modality
to open it.
"w" means that we want to open the file pippo.txt
to write on it.
Other modalities to open a file are the following:
"r" -> opens the file in reading modality
"r+" -> opens the file in reading+writing modality
"w" -> opens the file in writing modality
"w+" -> opens the file in reading+writing modality
"a" -> opens the file in writing modality and
insert the pointer at the end of file ("w" at
the start)
"a+" -> opens the file in reading+writing modality
Now we want to write in the file the string "Hello!!!",
therefore we use the following code:
$string="Hello!!!";
fwrite($write_file,$string);
fclose($write_file);
The istruction fwrite writes the string $string in the file
pippo.txt. fwrite has, as first argument, the pointer that
we used to open the file and, as second argument, the string
that we want to write on the file. fclose needs to close
the interaction with the file.
Reading on files
Now we want to read the string that we have written in
the file.
To do it, before all, we must open the file in reading modality,
therefore we write the following line of code:
$read_file=fopen("pippo.txt","r");
$read_file is a pointer that use the fopen instruction to
open the file pippo.txt in reading modality this time.
To read the content of file we must write the following
code:
$dim_file=filesize("pippo.txt");
$content=fread($read_file,$dim_file);
fclose($read_file);
T o read the complete content of the file we must know his
dimension, therefore we use the instruction filesize that
calculates the dimension (in bytes) of the file that has
in his argument. The instruction fread open the file to
read his content. fread has, as first argument, the pointer
to open the file in reading modality and, as second argument,
the portion of file that we want to read, in this case all
the file. Then fread stores the content of file in the variable
$content.
Even in this case fclose closes the interaction with the
file.
If we want to print on screen the content of the file pippo.txt,
we need to write easily:
echo $content;
On the screen it will be displayed the string Hello!!!
Complete code
The complete code to read and write on a file, as seen
in this article, is the following, remembering to you that
the simbols // are used to introduce a comment in php:
// String to write on file
$string="Hello!!!";
// Writing on file
$write_file=fopen("pippo.txt","w");
fwrite($write_file,$string);
fclose($write_file);
// Reading on file
$raed_file=fopen("pippo.txt","r");
$dim_file=filesize("pippo.txt");
$content=fread($read_file,$dim_file);
fclose($read_file);
// Printing on screen the content of file
echo $contenuto;