PmWiki | Cookbook / IncludeFile (original) (raw)

Summary: How to include an external file (from the same file system)

Version: 2025-12-01

Prerequisites: PHP 8, PmWiki 2.5 or greater

Status: Stable

Question

How do I include an external file (from the same file system)?

Answer

There are two simple and quick solutions, and one more elaborate and flexible recipe that should be the safest choice.

IncludeFile recipe - sanitised and filtered and access restricted for showing file content

This recipe includes the content of a given text file (or set of text files) in the output of the wiki page. "In the output" means that the inclusion is dynamic (like a link) - the markup in the wiki page itself remains unchanged. It is assumed that you have the CookbookSecure Attachments]] recipe in place, so uploaded files are gathered in a non-webserver-accessible space (usually below upload/) and grouped according to wiki page.

The basic syntax is

(:includefile filename.txt ext= header= fontsize=:)

The file is taken from the upload directory corresponding to the wiki page where the markup is placed in. You can also specify a set of files, using the ext=txt option (then omit the filename argument), any other file extension is of course also possible. The full set of options is:

**Security issues:**On inclusion it is checked whether the current user has permission to access the file, using the includefile action. This action is by default mapped to the read level, so the user needs read permission on the wiki page the file is belonging to. When including the contents all tags are effectively escaped by applying the PHSC ()) function to escape html special characters. The result is packed into a <code> environment.

ThomasP March 19, 2007, at 12:04 AM

Installation

Activate the IncludeFile recipe. https://pmwiki.org/wiki/Cookbook/IncludeFile

include_once("$FarmD/cookbook/includefile.php");

Script solutions

Unfiltered unsanitised inclusion of any file

Use only if you can trust all wiki editors. Do not use on a public wiki!

The following markup lets you include any file, relative to the path you set in the markup definition. Any file on the system can be included, if you give the right relative path in the markup. Note that files are not filtered or sanitised for output. Code may be interpreted. This can be potentially disastrous.

Markup('includefile', 'directives', '/\(:includefile\s+([-\/.\w]+)\s*:\)/', "mu_includefile"); function mu_includefile($m){ return Keep(implode('', file('/home/yourlocalfilesystempath/public_html/inc/'.$m[1]))); }

With this in place, the markup (:includefile something.html:) would include the contents of /home/yourlocalfilesystempath/public_html/inc/something.html in the output.

For a sanitised and preformatted output for showing code file content see next solution, although it is still allowing any file on the system to be shown. For a safe solution restricting files to be in a uploads directory and checking read permission see the recipe solution.

Sanitised and filtered for showing any file content

Use only if you can trust all wiki editors. Do not use on a public wiki!

No attempt is made to ask for any read authorisation or restrict the file path to uploads/ or other defined directories. You could show content of any file on your system. This could be a good solution for someone wanting to show contents of php files from the cookbook directory for instance.

Place in config.php:

Markup('showsource', 'directives', "/\(:showsource\s+(.?)\s?:\)/", "ShowSource"); function ShowSource($m) { text=PHSC(filegetcontents(text = PHSC(file_get_contents(text=PHSC(filegetcontents(m[1])); return "

".Keep($text)."
";}

Use markup (:showsource pathtofile/filename.ext:) i.e. like (:showsource cookbook/newpagebox.php:)
The filepath is relative to the location of your pmwiki script.

HansB December 07, 2007, at 07:00 AM

Solution 2: hardcoded skin template inclusion

In a skin template file, you can use <!--file:path/to/template.html-->.
Code will be interpreted.
This solution is not for showing file content only.

Releases

Notes

See Also

Contributors

Comments

See discussion at IncludeFile-Talk

User notes : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.