g
Printer Friendly Version

editor  
BellaOnline's PHP Editor
 

The PHP fread( ), filesize( ) and file_get_contents( ) Functions

In our previous tutorial, we studied the PHP fopen( ) and fclose( ) functions which are used to open and close a file. Now that we have the file open, our next step is to do something with the contents in that file. In most cases, this next step will be to read the contents of the file into a variable and, you guessed it, we will use the fread( ), filesize( ) and file_get_contents( ) functions to do this. Let’s take a look at the fread( ) function first.

The fread() Function

$file_contents = fread($fp, filesize($filename));

Here is the same line of code added to the fopen( ) and fclose( ) functions from the last tutorial.

Basic Code

$filename = "full path/name_of_file.txt";
$fp = fopen($filename, "r") or die("Can't open file");
$file_contents = fread($fp, filesize($filename));
fclose($fp);


Example

$filename = “catalog/products.txt”;
$fp = fopen($filename, "r") or die("Can't open file");
$file_contents = fread($fp, filesize($filename));
fclose($fp);


The fread( ) function takes two variables as arguments. Both of these variables are created in the two lines of code that come before the fread( ) function. The first argument is the $fp variable which was created with the fopen( ) function. This variable carries the information about the file pointer that was created with the fopen( ) function. The second argument is the $filename variable which contains the full path and name of the file that we wish to open. (You may wish to read the tutorial for the fopen( ) and fclose( ) functions.)

Because we already created these two variables in the last tutorial, we are ready to use the fread( ) function to read the contents of the file into the $file_contents variable. The fread( ) function uses the filesize( ) function to find the size of the file in bytes and then reads the file up to the length in bytes referenced by the file pointer from the fopen( ) function. Of course, you can name this variable that accepts the contents of the file anything that you wish but it is a good idea to give it a name, such as $file_contents, that will indicate the purpose of the variable.

The file_get_contents( ) Function

The file_get_contents( ) function is similar to the fread( ) function in that they both place or return the contents of the file into a string. The difference between them is that the file_get_contents( ) function is considered more efficient and does not require the fread( ) function.

$file_contents = file_get_contents($filename);





This site needs an editor - click to learn more!

PHP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2013 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor