logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

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

Guest Author - Diane Cipollo

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!

Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Twitter Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Facebook Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to MySpace Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Del.icio.us Digg The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Yahoo My Web Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Google Bookmarks Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Stumbleupon Add The+PHP+fread%28+%29%2C+filesize%28+%29+and+file%5Fget%5Fcontents%28+%29+Functions to Reddit




RSS | Related Articles | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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 BellaOnline Administration for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor