Reading a Whole File: file_get_contents


Reading a Whole File: file_get_contents

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




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net