Filesystem Helper

The Directory Helper file contains functions that assist in working with directories.

Loading this Helper

This helper is loaded using the following code:

helper('filesystem');

Available Functions

The following functions are available:

directory_map($source_dir[, $directory_depth = 0[, $hidden = FALSE]])
パラメータ:
  • $source_dir (string) -- Path to the source directory
  • $directory_depth (int) -- Depth of directories to traverse (0 = fully recursive, 1 = current dir, etc)
  • $hidden (bool) -- Whether to include hidden directories
戻り値:

An array of files

戻り値の型:

array

Examples:

$map = directory_map('./mydirectory/');

注釈

Paths are almost always relative to your main index.php file.

Sub-folders contained within the directory will be mapped as well. If you wish to control the recursion depth, you can do so using the second parameter (integer). A depth of 1 will only map the top level directory:

$map = directory_map('./mydirectory/', 1);

By default, hidden files will not be included in the returned array. To override this behavior, you may set a third parameter to true (boolean):

$map = directory_map('./mydirectory/', FALSE, TRUE);

Each folder name will be an array index, while its contained files will be numerically indexed. Here is an example of a typical array:

Array (
        [libraries] => Array
                (
                        [0] => benchmark.html
                        [1] => config.html
                        ["database/"] => Array
                                (
                                        [0] => query_builder.html
                                        [1] => binds.html
                                        [2] => configuration.html
                                        [3] => connecting.html
                                        [4] => examples.html
                                        [5] => fields.html
                                        [6] => index.html
                                        [7] => queries.html
                                )
                        [2] => email.html
                        [3] => file_uploading.html
                        [4] => image_lib.html
                        [5] => input.html
                        [6] => language.html
                        [7] => loader.html
                        [8] => pagination.html
                        [9] => uri.html
                )

If no results are found, this will return an empty array.

write_file($path, $data[, $mode = 'wb'])
パラメータ:
  • $path (string) -- File path
  • $data (string) -- Data to write to file
  • $mode (string) -- fopen() mode
戻り値:

TRUE if the write was successful, FALSE in case of an error

戻り値の型:

bool

Writes data to the file specified in the path. If the file does not exist then the function will create it.

例:

$data = 'Some file data';
if ( ! write_file('./path/to/file.php', $data))
{
        echo 'Unable to write the file';
}
else
{
        echo 'File written!';
}

You can optionally set the write mode via the third parameter:

write_file('./path/to/file.php', $data, 'r+');

The default mode is 'wb'. Please see the PHP user guide for mode options.

注釈

In order for this function to write data to a file, its permissions must be set such that it is writable. If the file does not already exist, then the directory containing it must be writable.

注釈

The path is relative to your main site index.php file, NOT your controller or view files. CodeIgniter uses a front controller so paths are always relative to the main site index.

注釈

This function acquires an exclusive lock on the file while writing to it.

delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])
パラメータ:
  • $path (string) -- Directory path
  • $del_dir (bool) -- Whether to also delete directories
  • $htdocs (bool) -- Whether to skip deleting .htaccess and index page files
戻り値:

TRUE on success, FALSE in case of an error

戻り値の型:

bool

Deletes ALL files contained in the supplied path.

例:

delete_files('./path/to/directory/');

If the second parameter is set to TRUE, any directories contained within the supplied root path will be deleted as well.

例:

delete_files('./path/to/directory/', TRUE);

注釈

The files must be writable or owned by the system in order to be deleted.

get_filenames($source_dir[, $include_path = FALSE])
パラメータ:
  • $source_dir (string) -- Directory path
  • $include_path (bool) -- Whether to include the path as part of the filenames
戻り値:

An array of file names

戻り値の型:

array

Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.

例:

$controllers = get_filenames(APPPATH.'controllers/');
get_dir_file_info($source_dir, $top_level_only)
パラメータ:
  • $source_dir (string) -- Directory path
  • $top_level_only (bool) -- Whether to look only at the specified directory (excluding sub-directories)
戻り値:

An array containing info on the supplied directory's contents

戻り値の型:

array

Reads the specified directory and builds an array containing the filenames, filesize, dates, and permissions. Sub-folders contained within the specified path are only read if forced by sending the second parameter to FALSE, as this can be an intensive operation.

例:

$models_info = get_dir_file_info(APPPATH.'models/');
get_file_info($file[, $returned_values = ['name', 'server_path', 'size', 'date']])
パラメータ:
  • $file (string) -- File path
  • $returned_values (array) -- What type of info to return
戻り値:

An array containing info on the specified file or FALSE on failure

戻り値の型:

array

Given a file and path, returns (optionally) the name, path, size and date modified information attributes for a file. Second parameter allows you to explicitly declare what information you want returned.

Valid $returned_values options are: name, size, date, readable, writeable, executable and fileperms.

symbolic_permissions($perms)
パラメータ:
  • $perms (int) -- Permissions
戻り値:

Symbolic permissions string

戻り値の型:

string

Takes numeric permissions (such as is returned by fileperms()) and returns standard symbolic notation of file permissions.

echo symbolic_permissions(fileperms('./index.php'));  // -rw-r--r--
octal_permissions($perms)
パラメータ:
  • $perms (int) -- Permissions
戻り値:

Octal permissions string

戻り値の型:

string

Takes numeric permissions (such as is returned by fileperms()) and returns a three character octal notation of file permissions.

echo octal_permissions(fileperms('./index.php')); // 644
set_realpath($path[, $check_existance = FALSE])
パラメータ:
  • $path (string) -- Path
  • $check_existance (bool) -- Whether to check if the path actually exists
戻り値:

An absolute path

戻り値の型:

string

This function will return a server path without symbolic links or relative directory structures. An optional second argument will cause an error to be triggered if the path cannot be resolved.

Examples:

$file = '/etc/php5/apache2/php.ini';
echo set_realpath($file); // Prints '/etc/php5/apache2/php.ini'

$non_existent_file = '/path/to/non-exist-file.txt';
echo set_realpath($non_existent_file, TRUE);    // Shows an error, as the path cannot be resolved
echo set_realpath($non_existent_file, FALSE);   // Prints '/path/to/non-exist-file.txt'

$directory = '/etc/php5';
echo set_realpath($directory);  // Prints '/etc/php5/'

$non_existent_directory = '/path/to/nowhere';
echo set_realpath($non_existent_directory, TRUE);       // Shows an error, as the path cannot be resolved
echo set_realpath($non_existent_directory, FALSE);      // Prints '/path/to/nowhere'