Reading a Whole File: file_get_contentsDo you simply want to read the entire contents of a file into a string, as we've been doing? Here's an easy way: use the file_get_contents function. It's easy; just pass the name and path of the function to work with (no file handle needed), as you see in phpfilegetcontents.php, Example 7-9. Example 7-9. Using file_get_contents, phpfilegetcontents.php<HTML> <HEAD> <TITLE> Reading a file with file_get_contents </TITLE> </HEAD> <BODY> <CENTER> <H1> Reading a file with file_get_contents </H1> <?php $text = file_get_contents("file.txt"); $br_text = str_replace("\n", "<BR>", $text); echo $br_text; ?> </CENTER> </BODY> </HTML> You can see this at work in Figure 7-9. Very simple. Figure 7-9. Reading from a file using file_get_contents. |