(PHP 4 , PHP 5)

highlight_file -- Syntax highlighting of a file


mixed highlight_file ( string filename [, bool return])

The highlight_file() function prints out a syntax highlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP.

If the second parameter return is set to TRUE then highlight_file() will return the highlighted code as a string instead of printing it out. If the second parameter is not set to TRUE then highlight_file() will return TRUE on success, FALSE on failure.

Note: The return parameter became available in PHP 4.2.0. Before this time it behaved like the default, which is FALSE


Care should be taken when using the show_source() and highlight_file() functions to make sure that you do not inadvertently reveal sensitive information such as passwords or any other type of information that might create a potential security risk.

Note: Since PHP 4.2.1 this function is also affected by safe_mode and open_basedir.

To setup a URL that can code hightlight any script that you pass to it, we will make use of the "ForceType" directive in Apache to generate a nice URL pattern, and use the function highlight_file() to show a nice looking code list.

In your httpd.conf you can add the following:

Example 1. Creating a source highlighting URL

<Location /source>
    ForceType application/x-httpd-php

And then make a file named "source" and put it in your web root directory.

<title>Source Display</title>
<body bgcolor="white">
= getenv("PATH_TRANSLATED");
    if (!
$script) {
"<br /><b>ERROR: Script Name needed</b><br />";
    } else {
        if (
ereg("(\\.php|\\.inc)$", $script)) {
"<h1>Source of: " . getenv("PATH_INFO") . "</h1>\n<hr />\n";
        } else {
"<h1>ERROR: Only PHP or include script names are allowed</h1>";
"<hr />Processed: " . date("Y/M/d H:i:s", time());

Then you can use a URL like the one below to display a colorized version of a script located in "/path/to/script.php" in your web site.


See also highlight_string().

Sites of interest: Web Hosting : Reseller Hosting : Website Hosting : HTML Editor : Web Design Templates : Free Web Hosting : ASP code examples : PHP & MySQL Code Examples
  Copyright 2004 Evrsoft Developer Network. Privacy policy - Link to Us

Contact Evrsoft