Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.1     Reading files

This 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.





<< 8 Files   8.1.1 readfile() >>
Table of Contents
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!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow