7
In PHP, there are numerous ways to read the contents of a file.
There are the functions fopen
, file
, file_get_contents
, readfile
, and even a class called SplFileObject
.
file
reads line by line and puts them in aarray
.fopen
reads the file and creates aresource
to be manipulated with other functions, such asfgets
andfwrite
.Splfileobject is similar to
fopen
and related, but you have all this merged with the functionality ofIterators
.
But I’d like to know the difference between readfile
and file_get_contents
. For I know that the latter read all the contents of the archive, unlike the others.
But why then have the two functions? Is there any difference between them?
This last byte number information I didn’t know +1
– Wallace Maxters
I upgraded it there for the
file_get_contents
, because if it doesn’t look like he "is bad" and thatreadfile
"it’s good".– Wallace Maxters
@Wallacemaxters wouldn’t be
json_encode()
? Decode vc generally stores, the Encode usually does not. ex:$json = json_encode($str); echo $json;
, flippedreadfile(json_encode($str))
that would be it?– rray
No, I mean if you want to decode a file
json
– Wallace Maxters
For example, it is useful to open the
composer.json
and turn it into aarray
in Php :)– Wallace Maxters
@Wallacemaxters now I understand, haha I’m with the
readfile()
in the xD head if it was to make afopen()/fgets()
in placefile_get_contetns()
to decode the json was to chip.– rray