Class: FileManager

Provides an API for managing common file operations such as listing directory contents, renaming, and deleting files. Using this class, extended file properties will be correctly re-linked to files that are renamed or moved, and file and folder names and paths will be verified to be correct.

Also note that file and folder paths are specified relative to FileManager::root().

Usage:

<?php

// Get listing of files/homepage directory
$list = FileManager::dir ('homepage');

// Delete files/homepage/photo1.jpg
$res = FileManager::unlink ('homepage/photo1.jpg');

?>

Properties

public static $root

The path to the root directory to store files.

public static $webroot

The web path to the root directory.

public static $error

The error message if an error occurs in one of the static methods.

Methods

public static root ()

Returns the $root. Sets $root if not yet set.

public static add_webroot ($path)

Adds the $webroot to a file/folder path, if not present. Examples:

foo.txt  -> /files/foo.txt
/foo.txt -> /files/foo.txt

public static strip_webroot ($path)

Removes the $webroot from a file/folder path. Examples:

/files/foo.txt -> foo.txt
files/foo.txt  -> foo.txt

public static error ()

Returns the last error message.

public static dir ($path = '')

List all directories and files in a directory. Returns an array with 'dirs' and 'files'. Each directory has a 'name', 'path', and 'mtime'. Each file also has 'fsize'.

public static unlink ($file)

Delete a file.

public static rename ($file, $new_name)

Rename a file or folder.

public static move ($file, $folder)

Move a file to a new folder.

public static touch ($file)

Touch a file. If it exists, updates its modification time. If not, creates a blank file.

public static mkdir ($folder)

Make a new folder.

public static rmdir ($folder, $recursive = false)

Remove a folder. The folder must be empty, or recursive must be set to true to remove non-empty folders.

private static rmdir_recursive ($path)

Handles recursively deleting folders for FileManager::rmdir().

public static list_folders ($path = '')

Returns a list of folders recursively under the specified folder path.

public static verify_folder ($path, $root = false)

Verify that the specified folder is valid, and exists inside a certain root folder.

public static verify_file ($path, $root = false)

Verify that the specified file is valid, and exists inside a certain root folder.

public static verify_folder_name ($name)

Verify that a folder name contains only a-z, A-Z, 0-9, underscores, and dashes.

public static verify_file_name ($name)

Verify that a file name contains only a-z, A-Z, 0-9, underscores, and dashes, and a dot.

public static fsort ($a, $b)

Helper for sorting files by name. For use with usort().

public static props ($file)

Fetch all of the properties for the specified file.

public static prop ($file, $prop, $value)

Get or set a property for the specified file. Can also retrieve an array of a property for a list of files if $file is an array.

public static prop_rename ($file, $new_name, $folder = false)

Rename the properties for a file that has been renamed.

public static prop_delete ($file)

Delete the properties for a file that has been deleted.