Reading a file with the file get content function PHP
Reading a file or URI is a common practice, and, likerule, it is profitable to do immediately and in full. A line or symbolic reading is essential, but there are not so many reasons for this, and this is used in special cases.
The file get content function PHP reads a string from the beginning to the end of the file. Particularly interesting is its use, when the file is the address of a resource on the Web (URI).
Description of function
The file get content function receives PHP asparameter name of the file or URI and produces a string. In this example, the usage code is shown. The function is accessed four times. The first time a file is read that contains just text, the second time it reads another file, which, in addition to the text, has HTML tags.
The next pair of calls is made throughencoding conversion. This is not always necessary, but in cases where strange symbols appear at the output, you should apply the conversion obtained after reading the line, and the Russian letters will be visible. The example of file get contents PHP in the browser will be displayed as shown below.
The first two function calls are not translatedencoding in the desired format, so Russian letters are not displayed. The contents of both files (local-1.txt and local-2.txt) are the same. Naturally, both files have line feeds, but the browser does not parse them. The file get content function PHP reads everything as it is, but the second file has the required tags, and the lines are displayed as needed, not in one line. This circumstance is not important, because as a general rule the file is read as is, and the presence of tags in it is actual when the HTML page is read, and there is everything necessary. However, this circumstance, especially the fact of possible inconsistency of the encoding, is important to know.
Other function parameters
If you need to read something special,for example, only a part of the file, or start with some particular position in it, you can use the offset and quantity parameter. However, the file get content function PHP can in some cases make a mistake by reading not the wrong number, the wrong number or the wrong position. If a non-local file is read, then the specified offset and the number of bytes read will be valid. In all other cases, it is better to check the result, issued by the function. You can specify the file search option in the include path folder and the resource parameter for reading stream contexts.
The sequence of parameters is as follows:
- file name;
- search parameter;
- resource parameter;
It is not desirable to use the search parameter, althoughin some cases it is advisable. When reading both local and remote files or URIs, it's best to always know what is read and where. If the URI has special characters, such as a space, the URI should be encoded with the urlencode function.
Reading a file remotely
Here the function uses the same way. There are no significant differences except for only one circumstance: with the first two calls to PHP file get contents there is no error.
The third page reads the site page. This page is displayed in the current place of the browser, in which there are quite specific CSS rules, but not the rules of the site being opened. Therefore, the result of the code that reads the URI will not be true. However, reading usually pursues the purpose of parsing, rather than displaying them in the place of reading.
But this circumstance should be borne in mind. Reading a URI is not the content of the site, but only a specific address.
Function of reading and parsing
If the file name is incorrect or the URI is readan error is possible. It is easy to trace by the absence of the result of reading. With incorrect other parameters, the function generates a Boolean value of FALSE. The usual use of the reading function is entirely associated with the wholesale setting of site parameters. For example, reading the configuration file, user data, or configuration file. In all such cases:
- the result is precisely defined;
- structure of readable data too.
It is difficult to predict in advance when reading a URI thathow it is read, and whether it is read at all. It is important to clearly imagine that not always read should be displayed in the browser. It's one thing to process information, another thing is trying to control the processing of HTML content without special tools and precautions in the browser.