Example of Reading And Showing Text File In PHP
Php / / July 04, 2021
In this example We will see how to display a text file in PHP.
We have two ways, the include method, and the other the file reading method, using file or file_get_contents.
The include method is to normally include the text, and with the other method we can save the content of the text in a variable and be able to format it, or replace words with others, capitalize all letters, etc.
We will need to have a .txt file, we can use another name, we just have to change the parameter in the functions
1: Example with include
Code:php
# examplede.com
echo " Text file file.txt: ";
include ("file.txt");
As you will see, print the text as is, we will not be able to format it, it may be that in the browser, it does not make the line breaks, because in the txt it is not used
, but a common line break is used, we can use
in the .txt, or else, use the second example, which will allow us to format, and we can make a nl2br
2: Example with file_get_contents
Code:php
# examplede.com
$ file = file_get_contents ("file.txt"); // We save file.txt in $ file
$ file = ucfirst ($ file); // We give it a bit of a format
$ file = nl2br ($ file); // Transform all line breaks into tag
echo " Text file file.txt: ";
echo $ file;