PHP - English

The fopen function in PHP



A better method to open files is with fopen( ). This function gives
you more options that, such as setting whether the file is for reading only,
for writing to as well, and a few more options. Here’s an example (this assumes
that you’ve read the short introduction on the previous


$file_contents = fopen( “dictionary.txt”,
“r” );
print $file_contents;


Run this script and see what happens. You should see something like the following
printed out:

Resource id #2

Not quite what you were expecting! The reason is that fopen( ) doesn’t
actually read the contents of a file. All it does is to set a pointer
to the file you want to open. It then returns what’s call a file handle. All
you’re doing is telling PHP to remember the location of the file.

The “r” on the end means “open this file for reading only”.
We’ll see other options in a moment. But now that
you’ve told PHP to remember the location of the file you want to open, how do
you read the contents of the file?

One way is to use fgets( ). This will read a specified number of character
on a single line of text. It’s typically used to loop round and read each line
of text. In the example below, we’re printing out each line separately. When
you’re using fgets( ), you also need to check when the end of the file
has been reached. This is done with the inbuilt function feoffile,
end of file. Try the script out, then we’ll explain what’s


$file_handle = fopen(“dictionary.txt”, “r”);

while (!feof($file_handle)) {

$line_of_text = fgets($file_handle);
print $line_of_text . “<BR>”;




What you should find is that the contents are printed out on separate lines.
But how does it work?

The first line is this:

$file_handle = fopen( “dictionary.txt”,
“r” );

What we’re doing here is asking PHP to open up a file, and remember the location.
The location is stored as a file handle. We’re putting this into a variable
called $file_handle. So we haven’t yet read the contents of the file
– we’ve just asked PHP to remember where it is.

The next line is tricky! It’s a while loop:

while ( !feof( $file_handle ) ) {


There’s really two parts to this. There’s the while loop:

while () {


And then there’s the condition for the while loop:

!feof( $file_handle )

A while loop, remember, just goes round and round until you tell it to stop.
It goes round and round while a condition is true. The condition between the
round brackets was our strange !feof line.

The function feof( ) means “file end of file”. It tells PHP when
the end of a file has been reached. You put the file between the round brackets
of the function:

feof( $file_handle )

This means, end of the file referred to in the variable called $file_handle.
Except, we’ve used the NOT operator ( ! ):

!feof( $file_handle )

That’s because we want to keep looping while the end of the has NOT been reached:

while ( !feof( $file_handle ) ) {


This whole line, then, reads: “While the end of the file has NOT been
reached, loop round the file pointed to in $file_handle.” As soon as the
end of the file has been reached, the while loop will end.

Inside the while loop, the first line is this:

$line_of_text = fgets( $file_handle );

We’re using the fgets( ) function to get a line of text from our file. Again,
we need the file handle:

fgets( $file_handle );

So we get a line of text from our file, and then place the line into a variable.
We then print out the line of text:

print $line_of_text . “<BR>”;

As well as printing out the line of text, we’re adding a HTML line break.

The last line in the code is this:

fclose( $file_handle );

All this does is to close the open file. It tells PHP that the pointer to the
file is no longer needed. You should always close files that you have opened
with fopen().


The code is a bit tricky, when you’re meeting it for the first time. But this
kind of file opening is useful when you need to read each line of text. With
our file, for example, we could separate each half of the line. We might want
to put the abbreviations into one list box and the meanings into another.

Another point to bear in mind about fgets is that it can take (and often does)
a second argument – the size of the line to read:

fgets($file_handle, line_size);

The line size needs to be in bytes. The default is 1024. But this line size
is only optional in PHP version 4.2 and above. If your version is earlier than
this, then you may get an error if you miss out the line size:

fgets($file_handle, 1024);

If you’re really packing a lot of information into each line, then just increase
the number for line size.


In the next part, we’ll see other options for fopen, rather than the “r”


Kaynak : ‘sitesinden alıntı

Yorum Yap