Selecting a Mode of Access
Before working with a file, it must exist. Lets use the fopen() function to create and open a file:
resource fopen (string filename, string mode [, int use_include_path [, resource zcontext]])
The fopen() function will attempt to open a stream to the specified file. Using the correct mode parameter is very important at this point, as it identifies the type of file access that will be required. Here is a list of all possible mode parameters you can use with the fopen() function:
mode Description
'r' Open for reading only; place the file pointer at the
beginning of the file.
'r+'
Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' Open for reading and writing; place the file pointer at
the end of the file. If the file does not exist, attempt to create it.
'x' Create and open for writing only; place the file pointer
at the beginning of the file. If the file already exists, the fopen()
call will fail by returning FALSE and generating an error of level E_WARNING.
If the file does not exist, attempt to create it. This is equivalent to
specifying O_EXCL|O_CREAT flags for the underlying open(2)
system call. This option is supported in PHP 4.3.2 and later, and only works
for local files.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.
Tuesday, April 17, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment