Kohana ‘filesystem’ Helper

A robust helper to handle everything file and folder-related within Kohana

Posted on Wednesday, August 20th, 2008 at 12:49 pm

Kohana is just an excellent framework to work with, but one of my beefs is that the structure of its bundled libraries and helpers is a little haphazard, missing basic functions you'd expect to find in the core (so you can rely on them when writing modules) omitted!

For this reason, here's my attempt at a more integrated helper class. It's somewhat of a Frankenstein's monster with bits borrowed from Code Igniter, and my own code thrown in as well. Apart from this, it:

  • Adds a few new methods
  • Improves on some existing methods
  • Combines the 'download' helper functionality
  • Tidies up various method calls

The methods are:

  • map()
  • get_folders()
  • get_files()
  • delete_files()
  • read_file()
  • write_file()
  • make_path()
  • download()

Download filesystem.php here.

map ($path, $levels = NULL, $structured = FALSE, $files_first = FALSE)

Returns a hierarchical array of folders and files from the specified path.

The parameters are

  • path : string - path to the folder
  • [levels] : number - limit how many folders deep you want to recursively scan
  • [structured] : boolean - split each returned branch into '/folder' and '/files' sub-arrays
  • [files_first] : boolean - if not returning as a structured array, list files before folders

get_folders ($path, $appendPath = false)

Returns an array of folders from the specified path.

The parameters are

  • path : string - path to the folder
  • [append_path] : boolean - append the original path to the return output

get_files ($path, $mask = NULL, $appendPath = false)

Returns an array of files from the specified path.

The parameters are

  • path : string - path to the folder
  • [mask] : mixed - a file mask to determine which files to return. Can be either
    • null - all files
    • string - a single file extension
    • array - an array of file extensions
    • regular expression - any valid regular expression, for example '/^config/'
  • [append_path] : boolean - append the original path to the return output

delete_files ($path, $mask = NULL, $del_dir = FALSE, $del_root = FALSE)

Recursively deletes all files and optionally folders from the specified path.

The parameters are

  • path : string - path to the folder
  • [mask] : mixed - a file mask to determine which files to delete. Can be either
    • null - all files
    • string - a single file extension
    • array - an array of file extensions
    • regular expression - any valid regular expression, for example '/^config/'
  • [del_dir] : boolean - delete any directories encountered when prcoessing
  • [del_root] : boolean - delete the root directory after processing

read_file ($file)

Opens the specified file and returns its contents as a string.

The parameters are:

  • file: string - path to the folder

write_file ($path, $data, $append = FALSE)

Writes data to the specified file.

The parameters are

  • path : string - path to the folder
  • data : string - the data you want written to the file
  • [append] - boolean - optionally append data to the file, rather than overwriting it

make_path ($path, $permissions = 0755)

Recursively create folders to form the specified path.

The parameters are

  • path : mixed - path or an array of paths
  • [permissions]: number - permissions you want granted to the new folders

download ($filedata, $filename = NULL)

Download file content by popping up a download dialog in the user's browser.

The parameters are

  • filedata : mixed - can either be
    • filepath - the path to an existing file
    • string - data you want downloaded as a file
  • [filename] : string - a filename that will appear in the user's download dialog

2 Responses to “Kohana ‘filesystem’ Helper”

  1. Mark |

    So… how do I install and use this? You make no notes of that.

    What folder do I put it in? How do I access the functions?

  2. Dave Stewart |

    The same as you would any other Kohana Helper. Just stick it in your application/helpers directory and then call it as normal, for example:

    filesystem.make_path(’some/path/to/files’);

Make a comment or suggestion...

  • Anti-spam: enter the characters in the image (required).