Reading Files in PHP
In this section we are going to cover how to open and read file content in PHP.
| Click on a Topic [ Hide ] |
The famous fgets() function
| Learn PHP and MySQL fast! | ||
|
Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly... >> Order your copy now for just $39.95 << |
|
The fgets() functions is used to read a line from a file. Using this function we either read the entire line into a string or specify the number characters we like to read. Let’s looks at the function more closely...
fgets ($handle, $length);
The fgets() function has two parameter. The first parameter is the file pointer and the 2nd parameter which is optional is the number of bytes to read in one call. If the 2nd parameter is not giving, the function will read at the end of the line.
- $handle - the file pointer
- $length - number of bytes to read. If length is not specified it will read at the end of the line.
Note: If line exceeds 1024 characters, it is advisable to read only up to maximum of 1024 characters at a time for not using up lot of memory.
Now, let's look at some example of how to read from a file.
Reading entire line
The following code reads a line from a file into a string. The code will start off by placing the file pointer at the beginning of the file and then reading the first line in our file into a string.
<?php
$fh = fopen("myfile.txt", "r");
$line = fgets($fh);
echo $line;
fclose($fh);
?>
In the above we didn't specify how many bytes we want to read. Therefore, the entire line is read.
Remember, the mode 'r' means we are opening the file for reading only.
Reading number of bytes
In this second example, we use the same method to read only certain number of characters in a line.
<?php
$fh = fopen("myfile.txt", "r");
$str = fgets($fh, 64);
echo $str;
$line2 = fgets($fh, 64);
echo $str;
fclose($fh);
?>
The above code reads the first 64 characters and then reads the next 64 characters in the line.
Reading file line by line
Using the fgets() function we can also read the entire file line by line in one loop. Let's look at two different examples to see how we can do that.
Example 1 - reading file line by line
<?php
$fh = fopen("myfile.txt", "r");
while(true)
{
$line = fgets($fh);
if($line == null)break;
echo $line;
}
fclose($fh);
?>
Example 2 - reading file line by line
<?php
$fh = fopen("myfile.txt", "r");
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
}
fclose($fh);
?>
Reading entire file at once
In some cases you might need to read the entire file at once. Reading the file line by line is probably more work in that case.
In this case you can utilize other functions that will read the entire file content in a string in one go. Once such function is file_get_contents()
Let's look at an example of how to use this function.
<?php
$fh = fopen("myfile.txt", "r");
$file = file_get_contents("myfile.txt");
echo $file;
?>
The above code will read the entire file content of myfile.txt into the string $file.
Please note that if the file is fairly large, your script/application might hang due to lack of resources. It is not advisable to read big files in one go. Either you should split the file into two or more pieces or utilize more smart ways of reading a file.
Are you still stuck using HTML?
"Learn how to build websites using PHP and MYSQL"
| << PHP File Open | Top |



