首頁  >  文章  >  後端開發  >  如何應用fopen()函數的選擇模式?

如何應用fopen()函數的選擇模式?

黄舟
黄舟原創
2017-05-20 17:11:151806瀏覽

如何應用fopen()函數的選擇模式?

一、fopen()函數

在php中使用fopen()函數開啟一個文件,具體語法格式如下:

resource fopen ( string filename , string $mode [, int use_include_path[, resource zcontext ]] )

參數filename 指定開啟的檔案名稱。

注意:

參數filename可以使包含檔案路徑的檔案名稱(例如:「C:/Windows/php.ini」或「./php.ini」),可為了避免不同系統之間切換可能會帶來的麻煩,採用“/”作為路徑分隔符,參數filename 也可以是由魔種協議給出的URL (例如:“http://mrbccd.cn”或者“ftp://www.mrbccd.cn//”),如果指定URL 位址,則可以開啟遠端檔案。

參數 mode 設定開啟檔案的方法,參數值如下表:

##只讀讀取模式—進行讀取,檔案指標位於檔案的開頭r+讀寫讀寫模式—進行讀寫,文件指針位於文件的開頭。在現有文件的內容的最後之前進行寫入就會覆寫原有的W只寫寫模式—進行寫入文件,文件指標指向頭文件。如果檔案存在,則所有檔案內容被刪除,否則函數將建立這個檔案w+讀取寫入xx+#aa+bt

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~

mode 模式名稱 說明
#r
##讀寫模式—進行讀寫,文件指標指向頭檔。如果文件存在,則所有文件內容被刪除,否則函數將創建這個文件
謹慎寫 寫模式打開文件,從文件頭開始寫。如果檔案已經存在,則該檔案將不會被打開,函數傳回false,PHP將產生一個警告
謹慎寫入 讀/寫模式開啟文件,從文件頭開始寫。如果檔案已經存在,則該檔案將不會被打開,函數傳回false,PHP將產生一個警告
追加 追加模式開啟文件,文件指標指向尾文件,如果該文件已有內容,則將從文件末尾開始追加,如果該文件不存在,則函數將創建這個文件
追加 追加模式開啟文件,文件指標指向頭文件,如果該文件已有內容,則將從文件末尾開始追加或讀取,如果該文件不存在,則函數將建立這個檔案
二進位 #二進位模式—用來與其他模式連線。如果檔案系統能夠區分二進位檔案和文字文件,可能會使用它。 Windows可以區分;UNIX則不區分,建議使用這個選項,方便取得最大程度的可攜性。它是預設模式
文字 用於與其他模式的結合。這個模式只是 Windows下的選項

以上是如何應用fopen()函數的選擇模式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn