highlight_file

(PHP 4 >= 4.0.0)

highlight_file -- Syntax highlighting of a file

Description

bool highlight_file ( string filename)

The highlight_file() function prints out a syntax higlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP. Returns TRUE on success, FALSE on failure.

Note: 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.

Example 1. Creating a source highlighting URL

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:

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

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

<HTML>
<HEAD>
 php Manual>Source Display</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<?php
    $script = getenv ("PATH_TRANSLATED");
    if(!$script) {
    echo "<BR><B>ERROR: Script Name needed</B><BR>";
    } else {
    if (ereg("(\.php|\.inc)$",$script)) {
    echo "<H1>Source of: $PATH_INFO</H1>\n<HR>\n";
    highlight_file($script);
    } else {
    echo "<H1>ERROR: Only PHP or include script names are allowed</H1>"; 
    }
    }
    echo "<HR>Processed: ".date("Y/M/d H:i:s",time());
?>
</BODY>
</HTML></PRE
></TD
></TR
></TABLE
>
      </P
><P
>
       Then you can use an URL like the one below to display a colorized
       version of a script located in "/path/to/script.php" 
       in your web site.
      </P
><P
>
       <DIV
CLASS="informalexample"
><A
NAME="AEN41578"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>http://your.server.com/source/path/to/script.php</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><DIV
CLASS="tip"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>Tip: </B
>As with anything that outputs
its result directly to the browser, you can use the <A
HREF="ref.outcontrol.html"
>output-control functions</A
> to capture
the output of this function, and save it - for example - in a
<A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>.</P
></BLOCKQUOTE
></DIV
><P
>
     See also <A
HREF="function.highlight-string.html"
><B
CLASS="function"
>highlight_string()</B
></A
>,
     <A
HREF="function.show-source.html"
><B
CLASS="function"
>show_source()</B
></A
>.
    </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.get-browser.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="../index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.highlight-string.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>get_browser</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.misc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>highlight_string</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>