The fopen() function is used to open files in PHP.
--------------------------------------------------------------------------------
[color=FA0309]Opening a File[/color]
The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
The following example generates a message if the fopen() function is unable to open the specified file:
The fclose() function is used to close an open file:
The feof() function checks if the "end-of-file" (EOF) has been reached. The feof() function is useful for looping through data of unknown length.
The fgets() function is used to read a single line from a file. Note: After a call to this function the file pointer has moved to the next line. [color=FA0309]Example[/color]
The example below reads a file line by line, until the end of file is reached:
The fgetc() function is used to read a single character from a file.
The example below reads a file character by character, until the end of file is reached:
The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
The file may be opened in one of the following modes:<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
[color=FA0309]Note[/color]: If the fopen() function is unable to open the specified file, it returns 0 (false).
[color=FA0309]Example[/color]The following example generates a message if the fopen() function is unable to open the specified file:
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
</body>
</html>
--------------------------------------------------------------------------------
[color=FA0309]Closing a File[/color]<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
</body>
</html>
The fclose() function is used to close an open file:
<?php
$file = fopen("test.txt","r");//some code to be executedfclose($file);
?>
--------------------------------------------------------------------------------
[color=FA0309]Check End-of-file[/color]$file = fopen("test.txt","r");//some code to be executedfclose($file);
?>
The feof() function checks if the "end-of-file" (EOF) has been reached. The feof() function is useful for looping through data of unknown length.
[color=FA0309]Note[/color]: You cannot read from files opened in w, a, and x mode!
if (feof($file)) echo "End of file";
--------------------------------------------------------------------------------
[color=FA0309]Reading a File Line by Line[/color]The fgets() function is used to read a single line from a file. Note: After a call to this function the file pointer has moved to the next line. [color=FA0309]Example[/color]
The example below reads a file line by line, until the end of file is reached:
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
--------------------------------------------------------------------------------
[color=FA0309]Reading a File Character by Character[/color]$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
The fgetc() function is used to read a single character from a file.
[color=FA0309]Note[/color]: After a call to this function the file pointer moves to the next character.
[color=FA0309]Example[/color]The example below reads a file character by character, until the end of file is reached:
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>