Jump to content

print exact file contents


doddsey_65

Recommended Posts

is there a way to print the file as it appears. I want to print the HTML and PHP within a file rather than just the output.

 

Example: File contains:

 

<?php
echo "Hello World";
?>

 

i would like to echo:

 

<?php
echo "Hello World";
?>

 

instead of just hello world which is what i get with file_get_contents()

Link to comment
https://forums.phpfreaks.com/topic/221477-print-exact-file-contents/
Share on other sites

Plenty of ways of doing this

I'll give u 2

1)

<?
header('Content-Type: text/plain');
readfile('file.name');

 

note: that the use of header and content type, means this file is of that type, so u cant mix this with html.

 

1)

<?
$contents=htmlspecialchars(file_get_contents('file.name'));

Echo $contents;

 

using htmlspecialchars, to convert html/xml/php/etc file to html encoded file, where html special characters are converted to their meta equivalents, < becomes <

instead of just hello world which is what i get with file_get_contents()

 

How are you calling file_get_contents()? The function will not evaluate PHP code, it simply fetches the content of the file (or output of an URL) and nothing else.

thanks for the replies. I managed to solve it with highlight_string().

Does anyone know, however, if there is a function to highlight the html as well as php files? Or am I looking at adding the possible tags into arrays as keys with their respective colors as values?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.