I was wondering what the difference was between the file en the fopen function. I always use the file function to read a file, like this:
$data = file('file.txt');
I know that I can also use the fopen function to do the same, but what are the difference between these two function? Which one is the best to use?
file() loads the entire contents of the specified file into an array, with each line of the file becoming one array element.
fopen() allows you to open a file for reading or writing, so that you can then use fread() or fwrite() to read/write specific portions of the file, for example. When you're done with the file, you should call fclose() to close it and make it available to other programs/PHP scripts again.
file() is very convenient for grabbing the whole file at once, but if you only need a couple of lines out of a very large file it can be very wasteful. file() also doesn't let you write the file.
Helping Small Business Grow Online!