Php file Function
File
(PHP 4 and PHP 5)
File-read the entire file to an array
Description
A series of files (string $ file [summary $ flag = 0 [, resource $ background])
Read the entire file to an array.
Note: You can use file_get_contents () to return the file content as a string.
Parameters
File Name
File Path.
Prompt
The URL can be used as the file name and this function. If the package is enabled. For more information about the fopen () function, see how to specify the file name and list of supported protocols/encapsulation protocols, supported URL protocols.
Flag
An optional parameter can be a flag or a constant greater than the following:
FILE_USE_INCLUDE_PATH
Search for include_path in the file.
FILE_IGNORE_NEW_LINES
When a new row ends, each array element
FILE_SKIP_EMPTY_LINES
Skip empty rows
FILE_TEXT
The returned content is encoded in UTF-8. You can specify a different encoding to establish a custom range. This flag cannot be used for FILE_BINARY. This flag only applies to PHP 6.
FILE_BINARY
The content is changed to binary data. This is the default setting and cannot be used for FILE_TEXT. This flag only applies to PHP 6.
Background
The stream_context_create () function created by the background resource.
Note: PHP 5.0.0 can be added. For information, see the stream function.
Return Value
Returns an array in the file. The file in the row corresponding to the array of each element is still important to line breaks. If file () fails, FALSE is returned.
Note: The arrays generated in each row will include the end of the row, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim () If you do not want to end the line.
NOTE: If PHP does not correctly recognize the end of a line, whether reading a file or creating a Macintosh computer, enabling the auto_detect_line_endings runtime configuration option may help solve this problem.
Modify
Version description
6.0.0 added support for FILE_TEXT and FILE_BINARY flags.
5.0.0 background parameter added
The flag parameters earlier than 5.0.0 to PHP 5.0.0 only overwrite include_path and enable 1
4.3.0file () becomes binary Security
Instance
For example, file 1 ()
// Get a file into an array. In this example we'll go through HTTP to get
// The HTML source of a URL.
$ Lines = file ('HTTP: // www.example.com /');
// Loop through our array, show HTML source as HTML source; and line numbers too.
Foreach ($ lines as $ line_num => $ line ){
Echo "Line #{$ Line_num}: ". Htmlspecialchars ($ line )."
N ";
}
// Another example, let's get a web page into a string. See also file_get_contents ().
$ Html = implode ('', file ('HTTP: // www.example.com /'));
// Using the optional flags parameter since PHP 5
$ Trimmed = file('somefile.txt ', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
?>