Reading a file with the file get content PHP function

Reading a file or a URI is common practice, and as a rule, it is beneficial to do so immediately and in full. Line-by-line or character-by-character reading is essential, but there are not many reasons for this, and this is used in special cases.

file get content php

The file get content function PHP reads a line from the beginning to the end of the file. Its use is especially interesting when the resource is the address of a resource on the Web (URI).

Function Description

The file get content function PHP receives a file name or URI as a parameter and returns a string. This example shows the usage code. The function is accessed four times. The first time a file is read, which contains just text, the second time another file is read, which, in addition to text, has HTML tags.

file get content php

The next pair of calls is through encoding conversion. This is not always necessary, but in cases where strange characters are formed at the output, you should apply the conversion of the line received after reading, and the Russian letters will be visible. An example file get contents PHP in the browser will be displayed as shown below.

file get content php

The first two function calls do not translate the encoding into the desired format, so the 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 in the second file there are the necessary tags, and the lines are displayed as needed, and not in one line. This circumstance is not essential, because, as a general rule, the file is read as is, and the presence of tags in it is relevant when the HTML page is read, and there is everything you need there. However, this circumstance, especially the fact of a possible encoding mismatch, is important to know.

Other function parameters

If you need to read something in a special way, for example, only part of the file, or start from some specific position in it, you can use the offset and quantity parameter. However, the file get content PHP function in some cases can make a mistake by reading the wrong, in the wrong amount or from the wrong position. If a non-local file is being read, then the specified offset and the number of bytes to be read will be valid. In all other cases, it is better to control the result produced by the function. You can set the file search parameter in the include path folder and the resource parameter for reading streaming contexts.

file get contents php example

The sequence of parameters is as follows:

  • file name;
  • search parameter;
  • resource parameter
  • bias;
  • quantity.

It is not advisable to use the search parameter, although in some cases it is advisable. When reading both local and remote files or URIs, it is best to always know what is being read and where it comes from. If the URI has special characters, such as a space, the URI should be encoded with the urlencode function.

Reading file remotely

Here the function uses the same. There are no significant differences, with the exception of only one circumstance: there is no error with the first two calls to PHP file get contents.

file get content php

In the third call, the page of the site is read. This page is displayed in the current location of the browser, in which very specific CSS rules apply, but not the rules of the site being opened. Therefore, the result of code that reads the URI will not be true. However, reading usually serves the purpose of parsing, rather than displaying them in the place of reading.

file get content php

But this circumstance should be borne in mind. Reading a URI is by no means the content of a site, but only a specific address.

Reading function and parsing

An invalid readable file name or URI may result in an error. This is easy to track by the lack of reading result. If other parameters are incorrect, the function generates a Boolean value of FALSE. The usual use of the read function is wholly associated with the wholesale setting of site parameters. For example, reading a configuration file, user data, or settings file. In all such cases:

  • the result is precisely defined;
  • readable data structure too.

php file get contents error

When reading a URI, it is difficult to predict in advance what will be read and how, and whether it will be read at all. It is important to clearly understand that not always read should be displayed in the browser. Processing information is one thing, and trying to control the processing of HTML content without special tools and precautions in the browser is another thing.

Source: https://habr.com/ru/post/K11507/


All Articles