Display the Contents of a File Using PHP

How to Display the Contents of a File Using PHP

PHP has a built in function to display the contents of a file. This is helpful if you want to update a webpage to reflect some data that is regularly updated or output to a file. It is also helpful if you have access to the web server and it is easier for you to update a text file than it is to edit html. Simply point your webpage to the text file and the contents of the webpage get updated automatically.

Here’s how it works:

<?php 
echo file_get_contents('http://brockangelo.com/uptime.txt'); 
?>

Easy enough. I used to have a message in the footer of this webpage that said “This server has been up for 14 days, 7 hours, 22 minutes.” All that was doing was reading the contents of the text file that got updated every hour.

3 thoughts on “Display the Contents of a File Using PHP

  1. rubul

    i guess either of the functions fread; file_get_contents has limit on the number of bytes on the file;;;that it works well for small files but fails for large files–(executes the die statement(if it is added));;;;

    so what i wanted to know is that if there is a way to copy large file contents into a string????
    as fread fgets or get_file_contents fails what is the alternative???????

  2. Chad Buie

    Actually you could probably hold more data in an array….then just write a command to pull a limited amount of data and then a link to the rest of the article. That could work. It is what I am doing for my website.

Comments are closed.