By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Output a files content with PHP

Tutorial on how to output a files content with PHP.

Edited: 2012-05-11 00:57

To output a files content with , all we need to use is the file_get_contents function. We can use this function to save the file into a variable for later use.

$file = "my-text-file.txt";
$GetContent = file_get_contents($file)

echo $GetContent;

The content of the file should now be saved in the $GetContent variable, we can echo this whenever we want to.

Outputting directly to the Browser

When you don't need to work with the data otherwise, simply outputting the data directly might save you some memory. This can be done with the readfile function.


Sending the correct Headers

Its important that you also send the correct content-type headers, otherwise the result may look different then you expect. For png images this would be done like below.

 header('Content-Type: image/png');

See also:

  1. List of Content-types