PHP File Open/Read/Close
PHP Open File - fopen( )
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.
The first parameter of fopen() 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 also generates a message if the fopen() function is unable to open the specified file
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP Read File - fread( )
The fread( ) function reads from an open file.The first parameter of fread( ) contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
The following PHP code reads the "webdictionary.txt" file to the end: fread($myfile,filesize("webdictionary.txt"));
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
PHP Close File - fclose( )
The fclose( ) function is used to close an open file. It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!
The fclose( ) requires the name of the file (or a variable that holds the filename) we want to close:
In the following example $x is an integer. The PHP var_dump() function returns the data type and value:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Read Single Line - fgets( )
The code below uses file_exists function to determine if the file my_settings.txt exists.The fgets( ) function is used to read a single line from a file. After a call to the fgets( ) function, the file pointer has moved to the next line. The example below outputs the first line of the "webdictionary.txt" file:
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
PHP Check End-Of-File - feof()
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.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "
";
}
fclose($myfile);
?>
PHP Read Single Character - fgetc( )
The fgetc() function is used to read a single character from a file. The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached:
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
PHP Create File - fopen( )
The fopen( ) function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen( ) on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).
$myfile = fopen("testfile.txt", "w")
PHP File Permissions
If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.
PHP Write to File - fwrite( )
The fwrite( ) function is used to write to a file. The first parameter of fwrite( ) contains the name of the file to write to and the second parameter is the string to be written.
To declare a type for the function return, add a colon ( : ) and the type right before the opening curly ( { )bracket when declaring the function. In the following example we specify the return type for the function.
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
PHP Overwriting
Now that "newfile.txt" contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>