Kohana FileSystem helper

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

6 Comments

  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');

  3. neovive

    Very nice helper … you should add this to the new projects.kohanaphp.com site. Haven't seen you around the Kohana forums in a while … are you still using Kohana?

  4. Dave Stewart

    Hey Neovive,

    I've been on Flash and AJAX projects for the past while, but a personal project and another commercial project I need to do both need Kohana in the next short while, so you'll probably see a bit more of me!

    To be honest I was getting tired of the seemingly endless changes in the core, so I hope by now they've settled down.

    I'll check the projects site too – that sounds like a nice addition!

  5. neovive

    Things have definitely stabilized with Kohana — I was getting concerned as well. There are now separate 2.x and 3.x branches. The 2.x branch is very stable and is maintained by a separate team with the goal of long term support and stability. The 3.x branch is experimental and completely separate at the moment.

  6. Cambrian

    Great helper! i like it very much, thx a lot.

Leave a Comment