In PHP, there are numerous ways to read the contents of a file.
The functions fopen
, file
, file_get_contents
, readfile
, and even a class named SplFileObject
.
-
file
reads line by line and puts them inarray
. -
fopen
reads the file and creates aresource
to be manipulated with other functions such asfgets
andfwrite
. -
SplFileObject is similar to
fopen
and related, however you have it all merged with theIterators
functionality.
But I'd like to know what the difference is between readfile
and file_get_contents
. For I know that the latter read the whole contents of the file, unlike the others.
But why then have both functions? Is there any difference between them?