mode |
模式名稱 |
說明 |
#r | ##只讀 | 讀取模式—進行讀取,檔案指標位於檔案的開頭 |
r+ | 讀寫 | 讀寫模式—進行讀寫,文件指針位於文件的開頭。在現有文件的內容的最後之前進行寫入就會覆寫原有的 |
W | 只寫 | 寫模式—進行寫入文件,文件指標指向頭文件。如果檔案存在,則所有檔案內容被刪除,否則函數將建立這個檔案 |
w+ | 讀取寫入##讀寫模式—進行讀寫,文件指標指向頭檔。如果文件存在,則所有文件內容被刪除,否則函數將創建這個文件 |
|
x
謹慎寫 |
寫模式打開文件,從文件頭開始寫。如果檔案已經存在,則該檔案將不會被打開,函數傳回false,PHP將產生一個警告 |
|
x+
謹慎寫入 |
讀/寫模式開啟文件,從文件頭開始寫。如果檔案已經存在,則該檔案將不會被打開,函數傳回false,PHP將產生一個警告 |
|
#a
追加 |
追加模式開啟文件,文件指標指向尾文件,如果該文件已有內容,則將從文件末尾開始追加,如果該文件不存在,則函數將創建這個文件 |
|
a+
追加 |
追加模式開啟文件,文件指標指向頭文件,如果該文件已有內容,則將從文件末尾開始追加或讀取,如果該文件不存在,則函數將建立這個檔案 |
|
b
二進位 |
#二進位模式—用來與其他模式連線。如果檔案系統能夠區分二進位檔案和文字文件,可能會使用它。 Windows可以區分;UNIX則不區分,建議使用這個選項,方便取得最大程度的可攜性。它是預設模式 |
|
t
文字 |
用於與其他模式的結合。這個模式只是 Windows下的選項 |
|
The parameter use_include_path can be a selection parameter to determine whether to search for filename files in the directory defined by include_path (include_path option of php.ini). For example: set the value of the include_path option in the php.ini file to "E:\AppServ\ www\MR\Instance\07\", if you want the server to open the specified file in this path, set the value of the parameter use_include_path to 1 or TRUE.
Parameter context is called context, which is also an optional parameter. It is a specific option that sets the flow operation and is used to control the operation characteristics of the flow. Generally, only the default stream operation settings are required and there is no need to use this parameter.
2. Mode selection
Different operating systems have different line ending habits when inserting a new line in a text file The choice of line terminator must be suitable for the user's operating system. If the wrong line terminator is used, the output may be a bunch of garbled characters when the file is opened, so it is very important to use the line terminator correctly.
UNIX-based systems use \n as the line terminator, Windows-based systems use \r\n as the line terminator, and Macintosh-based systems use \r as the line terminator.
Under Windows, provide a text conversion flag ("t") to convert \n to \r\n; or you can use ("b") to set the binary mode so that the data will not be modified. Convert. Therefore, there are two options for using line terminators in Windows systems: one is to use the text conversion flag ("t") for conversion; the other is to use ("b") to set the mode parameter mode to binary.
The default conversion mode depends on SAPI and the PHP version used, so in order to facilitate porting, it is recommended to set the appropriate mode when applying the fopen() function.
If you want plain text files with \n as the line terminator to be valid in other applications (such as Notepad). Then it is recommended to use ("t") mode in the script. Use ("b") in other cases.
If you do not specify ("b") mode when operating a binary file, some strange content may be output. Therefore, considering the portability of the program, it is strongly recommended to open the file with fopen() Always use ("b") mode; also, it is recommended to rewrite code that depends on ("t") mode. Make it use the correct line terminators and the correct ("b") pattern.
We will introduce the selection mode of how to apply the fopen() function here. Friends can try it locally~