PHP file read methods
PHP file read
There are many different ways how you can read the content of a file.
Step 1.
First of all you need to open the file for reading using the fopen function. You need to provide the filename as parameter and you also need to define to open mode which is in case of reading the 'r'. Before any real reading begins you also need to check if file opening was success.
Step 1.
First of all you need to open the file for reading using the fopen function. You need to provide the filename as parameter and you also need to define to open mode which is in case of reading the 'r'. Before any real reading begins you also need to check if file opening was success.
Code:
$fileName = 'test.txt';
$fileRes = fopen($fileName,'r');
if ($fileRes === FALSE) {
echo 'Can not open file!';
}
You can also check the file readability before trying to open it using theis_readable function as follows:
Code:
$isReadable = is_readable($fileName);
Step 2.
So if the opening of the file was success then we can try to read some data from it. There are more solutions to do this:
So if the opening of the file was success then we can try to read some data from it. There are more solutions to do this:
- Read the file at once into a string at once
- Read the file at once into an array
- Read the file line by line
- Read the file block by block
Let's see them one by one. The most basic function to read a file is fread.
The syntax:
The syntax:
Code:
string fread ( resource $handle , int $length )
Using fread you can read the complete file at once if you set the length parameter as big as the file size. You can use the filesize() function to do this and so you get the file content:
Code:
$content = fread($fileRes, filesize($fileName));
You also can read the complete content by using the file_get_contents function like this:
Code:
$content = file_get_contents($fileName);
Don’t forget that in this case you don’t need to open the file before reading. Thefile_get_contents do everything for you, you only need to provide the filename as parameter. Of course a readability check is recommended also in this way.
If the file is huge and/or you don’t want to read the complete content at once you can use fread to get the file content block by block in the following way:
If the file is huge and/or you don’t want to read the complete content at once you can use fread to get the file content block by block in the following way:
Code:
while (!feof($fileRes)) {
$block = fread($fileRes, 8192);
}
You also can read the file content line by line using the fgets function.
Code:
while (!feof($fileRes)) {
$line = fgets($fileRes);
}
It is also possible to read the complete file at once into an array using the filefunction. In this case every line of the file will be an element of the array. So if your file has 10 lines of text then you will get a string array with 10 elements. Thefile function similar to file_get_contents doesn’t require to use fopen. You need to provide the filename instead of file resource as parameter.
Code:
$contentArray = file($fileName);
Step 3.
If you have used fopen to open a file you also need to close that file after you readed what you want. You can do this simple with fclose:
If you have used fopen to open a file you also need to close that file after you readed what you want. You can do this simple with fclose:
Code:
fclose($fileRes);
As summary:
You need to use fopen, fclose and a file resource with fread and fgets.
You can simply use file and file_get_contents with a filename and no fopen, fclose is needed.
You need to use fopen, fclose and a file resource with fread and fgets.
You can simply use file and file_get_contents with a filename and no fopen, fclose is needed.
0 comments:
Post a Comment