8.1 Reading filesThis is NOT the latest copy of this book; click here for the latest version.
There are three distinct ways to open and display files, and we will be looking at all of them - each have their uses. The simplest possible way is to call readfile(), which takes a filename as its first parameter and simply opens the file, outputs it to the screen, then closes it.
A more complicated way, but equally more powerful, is to use the file_get_contents() function, which loads the file specified as the first parameter and returns it as a string. The last way, using the fopen() function, is the most complicated and also the most powerful - we will get onto that later.
Author's Note: you do not need to know all three ways to read files - indeed, it is probably best to learn one and stick with it for your own code. However, having said that, you will almost certainly come across each of these three methods in other people's code, because everyone has their own method of getting things done. My advice to you is to learn all three, at least as far as knowing what they do, and learn one specific way for your own use. That way, at least you have got a vague idea when you see other code, and you know that you can look back here or in the PHP manual if you get stuck.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|