int fopen(file_name, mode)This function takes two strings as its arguments:
| Value | Description |
|---|---|
| a | Open the file for appending only. Data will be written to the end of the existing file; if the file does not exist, PHP will attempt to create it. |
| a+ | Open the file for appending and reading. Data will be written to the end of the existing file; if the file does not exist, PHP will attempt to create it. |
| r | Open the file for reading only. |
| r+ | Open the file for reading and writing. Data will be written at th beginning of the existing file. |
| w | Open the file for writing only. Any existing content of the file will be lost. If the file does not exist, PHP will attempt to create it. |
| w+ | Open the file for writing and reading. Any existing content of the file will be lost. If the file does not exist, PHP will attempt to create it. |
Function fopen() returns an integer value by which the open file can be referred to. This integer value is traditionally called file handler. In the case of an error this function returns false. Here is a typical example of fopen() usage:
$fp = fopen("/some_dir/mydata.db", "wb"); // open binary data file for writing
if( ! $fp )
echo "Error: cannot open the file.";
else{
// do something with the file content
}
We also can use HTTP or FTP paths in the file name; that is, we can open a web page or a binary file on
another server and work with it as with usual file:
if( ! ($fp=fopen("http://someserver.net/somepage.html", "r")) ){
echo "Error: cannot open the file.";
}
else{
// do something with the page
}
int fclose(file_handler)This function takes one argument a file handler of an open file and returns true (1) if it closed it successfully, otherwise it returns false (0).
int fpassthru(filehandler)This function reads the file from the current position to the very end and prints the content. This function also closes the file when the end of the file is reached. It returns true on success, false on failure. The following example demonstrates a complete PHP script that can open any file on the server or a web page:
There is another function readfile() that works almost as fpassthru(). The formal syntax is:
int readfile(file_name)This function takes name of the file as an argument, opens the file for reading, reads its content, prints it, and closes the file. It returns true on success, false on failure. Using this function the example above can be rewritten as:
if( ! readfile($_REQUEST["fname"]) ) echo "Error: cannot open file";The difference between these functions is:
string fgets(file_handler, length)it takes two arguments file_handler and length. This function will read either length-1 characters from the current position or all the characters till the closest end of the line on the file, whichever comes first. This is the only reason we put this function in the text information section. fgets() returns the string read as the result on success, false on failure. The following example reads three first lines from the file index.html:
if( $fp = fopen("index.html", "r") ){
echo "<form> <textarea rows=5 cols=80>";
echo fgets($fp, 1024);
echo fgets($fp, 1024);
echo fgets($fp, 1024);
echo "</textarea> </form>";
}
Please notice that function fgets() stops reading from a file when it sees the end of the line
even if it read less than length-1 characters.
string fread(file_handler, length)This function reads exactly length characters from the file with the file handler file_handler.
$filename = "c:\\files\\somepic.gif"; $fd = fopen($filename, "rb"); $contents = fread($fd, filesize($filename)); fclose($fd);The following example shows how to use these functions in your PHP scripts.
boolean feof(file_handler)This function takes file handler of an open file as an arguments and returns true if the end of the file has been reached, false otherwise. Usual way to use this function is:
$fp = fopen("data.txt", "r") or die("Error: cannot open file 'data.txt' for reading");
while( ! feof($fp) ){
$str = fgets($fp, 1024);
// do something with $str
}
Note: function die() prints the message and terminates the script.
array file(filename)This function takes only one argument the name of the file to be read, reads the file, and puts all its content into an result array. Each element of the returned array contains exactly one line from the file. Upon failure, file() returns false.
int fputs(file_handler, str_to_write[, length]) int fwrite(file_handler, str_to_write[, length])These functions take a file handler of an open file and a string to write into the file. The third argument length is optional. If the length argument is given, writing will stop after length bytes have been written or the end of str_to_write is reached, whichever comes first. The functions return the number of bytes written, or -1 on error.
We will use the same functions to add some information at the end of the file. If we need to add something to the end we open the file using mode "a". In the example below we open a database file and add information obtained from an HTML form:
$name = $_POST["name"];
$phone = $_POST["phone"];
$addr = $_POST["addr"];
$zip = $_POST["zip"];
$fp = fopen("data.db", "a");
if( !$fp ) die("Error: cannot open file for writing.");
fputs($fp, "$name\t$phone\t$addr\t$zip\n");
fclose($fp);
Special symbols \t and \n are used to output tabulation symbol and new line symbol
respectively.
The following code combines read and write functions to create a hit counter for a page:
int rewind(file_handler) int fseek(file_handler, offset[, whence]) int ftell(file_handler)All of these functions take file handler as an argument.
Function rewind() resets the current position of the pointer to the beginning of the file. It returns true on success, false on failure.
Function ftell() returns the integer number indicating the offset of the pointer from the beginning of the file. In other words, it returns the current position of the pointer.
Function fseek() moves the current position within the file. The second argument offset indicates the offset of the pointer from the beginning of the file. For example, fseek($fp, 0) does exactly the same thing as rewind(). The third argument whence may take only one of the three possible values:
fseek($fp, ftell($fp)+5); // or fseek($fp, 5, SEEK_CUR);
dir_halndler opendir(path) string readdir(dir_handler) void closedir(dir_handler) void rewinddir(dir_handler)Function opendir() takes one string argument the name of the directory to open and returns directory handler on success, false on failure. As directory name we can use
Function readdir() reads the next file name from the directory. If there is no more files left it returns false, otherwise it returns the name of the file. Function closedir() closes the directory. The typical way to get to know all files in the current directory would be:
$dp = opendir(".");
while( ! ($fname=readdir($dp)) )
echo "$fname<br>";
closedir($dir);
Function rewinddir() works very similar to file function rewind() allowing us to
get back to the beginning of the directory without closing and reopening it.
There are several more functions