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

8.1.1     readfile()

This is NOT the latest copy of this book; click here for the latest version.

int readfile ( string filename [, bool use_include_path [, resource context]])

If you want to output a file to the screen without doing any form of text processing on it whatsoever, readfile() is the easiest function to use. When passed a filename as its only parameter, readfile() will attempt to open it, read it all into memory, then output it without further question. If successful, readfile() will return an integer equal to the number of bytes read from the file.

If unsuccessful, readfile() will return false, and there are quite a few reasons why it may file. For example, the file might not exist, or it might exist with the wrong permissions

Here is an example script:

<?php
    readfile
("/home/paul/test.txt");
    
readfile("c:\\boot.ini");
?>

The first example will work on most Unix-like systems, and will attempt to open the file test.txt in Paul's home directory - you will almost certainly want to change this to point to somewhere that exists and you have access to. The second example is for Windows systems, and should work in any Windows NT-descended OS, including Windows 2000, and XP. If you are using Windows 98/ME, you should try another file - just remember to keep double-slashing backslashes!

The advantages to using readfile() are clear: there is no fuss, and there is little way for it to go wrong. However, the disadvantage is equally clear: you have got absolutely no control over the text that comes out.

You should use readfile() when you just want to print a file out without any further action on your behalf - it is a powerful little one-liner.

Note that from here on in I will be using the variable $filename to signify a filename you have chosen. This is to avoid having to keep printing separate examples for Windows and Unix.





<< 8.1 Reading files   8.1.2 file_get_contents() and file() >>
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
A PHP User - 20 Aug 2008

If unsuccessful, readfile() will return false, and there are quite a few reasons why it may file.

Should be?

If unsuccessful, readfile() will return false, and there are quite a few reasons why it may fail.



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