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

8.1.2     file_get_contents() and file()

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

string file_get_contents ( string filename [, bool use_include_path [, resource context]])

array file ( string filename [, bool use_include_path [, resource context]])

The next evolutionary step up from readfile() is just called file_get_contents(), and also takes one parameter for the filename to open. This time, however, it does not output any data - instead, it will return the contents of the file as string, replete with new line characters \n where appropriate

Here is file_get_contents() in use:

<?php
    $filestring
= file_get_contents($filename);
    print
$filestring;
?>

$Filename, as mentioned already, is a variable used to represent a file you have chosen already, whether that be on Unix or Windows, which means that file_get_contents() opens that file and places its contents into $filestring. Effectively that piece of code is the same as our call to readfile(), but only because we're not doing anything with $filestring once we have it.

Consider this script:

<?php
    $filestring
= file_get_contents($filename);
    
$filearray = explode("\n", $filestring);

    while (list(
$var, $val) = each($filearray)) {
        ++
$var;
        
$val = trim($val);
        print
"Line $var: $val<BR />";
    }
?>

This time we use explode() to turn $filestring into an array, which is then iterated through, outputting one line at a time with line numbers. Remember that array indices start at 0, so we need ++$var to make sure that it starts at line 1 rather than line 0. Also, note that we call trim() on $val - this is because each element in the array still has its new line character \n at the end, and trim() will take that off.

File_get_contents() is an excellent general-purpose file-handling function that you will likely find yourself using extensively.

As an alternative, if you find yourself always wanting your files inside arrays, you can use the file() function - it works in the same manner as file_get_contents(), with the exception that it returns an array, which each line in the file returned as an element.





<< 8.1.1 readfile()   8.1.3 fopen() and fread() >>
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 - 30 Aug 2008

fuck them all

A PHP User - 30 Aug 2008

I need a help.I have *.vcf file,that I want to read and the data that this file content to put into Db.

A PHP User - 30 Aug 2008

I need a help.I have *.vcf file,that I want to read and the data that this file content to put into Db.

A PHP User - 30 Aug 2008

I have a file that is in HTML format that I want to use as my content for my webpage. I am trying to read the file into a variable and then display it using file_get_contents().

The output seems to only show the the first few lines of code which create a table.

Is there another way of doing this?

Thanks

A PHP User - 30 Aug 2008

I have a file that is in HTML format that I want to use as my content for my webpage. I am trying to read the file into a variable and then display it using file_get_contents().

The output seems to only show the the first few lines of code which create a table.

Is there another way of doing this?

Thanks

A PHP User - 30 Aug 2008

Nevermind. I take that back. I forgot that trim() only takes it off the beginning and end of strings. *oops*

A PHP User - 30 Aug 2008

Wouldn't that trim() function also take out all the spaces and tabs in your code? I'm pretty sure it would. I'd just use $filearray = str_replace("/n","",$filearray). That should work for taking out the newlines, but not spaces.

Hari - 30 Aug 2008

It would be more than helpful if you explain the use and usage of the functions before using them in your example code. Otherwise the book is really helpful.

CuBiC - 30 Aug 2008

I keep noticing that you use implode() and explode(), yet I can't say I've seen you explain it's use anywhere.
Maybe you should add that somewhere? =)



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 five plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow