Jump to main content Jump to doc navigation

What is modFileHandler?

modFileHandler is a service class used in MODX Revolution for handling files. It abstracts basic file management actions to provide helper methods for file management.

modFileHandler, modFile and modDirectory are still in their infancy stages. Many more methods will be added to them in Revolution 2.2.

Using modFileHandler

The basic idea behind modFileHandler is its "make" method. When passed a path into modFileHandler->make(), it will return either a modDirectory or modFile object, depending on what was passed inside of it.

For example, a simple snippet that makes a modDirectory object out of the passed "path" property (defaulting to "/www/test/") and then removes the directory:

if (!isset($path)) $path = '/www/test/';

$directory = $modx->fileHandler->make($path);
if (!is_object($directory) || !($directory instanceof modDirectory)) return 'Not a directory!';

if (!$directory->remove()) {
   return 'Could not remove directory.';

You can also create modDirectory or modFile objects from non-existent paths. This will allow you to run ->create() on them, allowing you to make new directories or files. For example, to create a new file with the content of 'Hello!' at the path "/www/test/test.txt":

$file = $modx->fileHandler->make('/www/test/test.txt');
if (!$file->create('Hello!')) {
   return 'File not written.';
return 'File written.';

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.


  • modmore
  • Jens Wittmann – Gestaltung & Entwicklung
  • Raffy
  • Digital Penguin
  • eydolan
  • deJaya
  • Following Sea
  • Sepia River Studios
  • Fabian Christen
  • Dannevang Digital
  • Anton Tarasov
  • Nick Clark
  • Lefthandmedia
  • Murray Wood
  • YJ
  • Chris Fickling
  • CrewMark
  • Richard


$292 per month—let's make that $500!

Learn more