Filesystem Helper

The Directory Helper file contains functions that assist in working withdirectories.

Loading this Helper

This helper is loaded using the following code:

  1. helper('filesystem');

Available Functions

The following functions are available:

  • directorymap($sourcedir[, $directory_depth = 0[, $hidden = FALSE]])

Parameters:

  • $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 directoriesReturns:An array of filesReturn type:array

Examples:

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

Note

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

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

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

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

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

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

  1. Array (
  2. [libraries] => Array
  3. (
  4. [0] => benchmark.html
  5. [1] => config.html
  6. ["database/"] => Array
  7. (
  8. [0] => query_builder.html
  9. [1] => binds.html
  10. [2] => configuration.html
  11. [3] => connecting.html
  12. [4] => examples.html
  13. [5] => fields.html
  14. [6] => index.html
  15. [7] => queries.html
  16. )
  17. [2] => email.html
  18. [3] => file_uploading.html
  19. [4] => image_lib.html
  20. [5] => input.html
  21. [6] => language.html
  22. [7] => loader.html
  23. [8] => pagination.html
  24. [9] => uri.html
  25. )

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

  • writefile($path, $data[, $mode = 'wb'_])

Parameters:

  • $path (string) – File path
  • $data (string) – Data to write to file
  • $mode (string) – fopen() modeReturns:TRUE if the write was successful, FALSE in case of an errorReturn type:bool

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

Example:

  1. $data = 'Some file data';
  2. if ( ! write_file('./path/to/file.php', $data))
  3. {
  4. echo 'Unable to write the file';
  5. }
  6. else
  7. {
  8. echo 'File written!';
  9. }

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

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

The default mode is ‘wb’. Please see the PHP user guidefor mode options.

Note

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

Note

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

Note

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

  • deletefiles($path[, $deldir = FALSE[, $htdocs = FALSE]])

Parameters:

  • $path (string) – Directory path
  • $del_dir (bool) – Whether to also delete directories
  • $htdocs (bool) – Whether to skip deleting .htaccess and index page filesReturns:TRUE on success, FALSE in case of an errorReturn type:bool

Deletes ALL files contained in the supplied path.

Example:

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

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

Example:

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

Note

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

  • getfilenames($sourcedir[, $include_path = FALSE])

Parameters:

  • $source_dir (string) – Directory path
  • $include_path (bool) – Whether to include the path as part of the filenamesReturns:An array of file namesReturn type:array

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

Example:

  1. $controllers = get_filenames(APPPATH.'controllers/');
  • getdir_file_info($sourcedir, $top_level_only)

Parameters:

  • $source_dir (string) – Directory path
  • $top_level_only (bool) – Whether to look only at the specified directory (excluding sub-directories)Returns:An array containing info on the supplied directory’s contentsReturn type: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 readif forced by sending the second parameter to FALSE, as this can be an intensiveoperation.

Example:

  1. $models_info = get_dir_file_info(APPPATH.'models/');
  • getfile_info($file[, $returnedvalues = ['name', 'server_path', 'size', 'date']])

Parameters:

  • $file (string) – File path
  • $returned_values (array|string) – What type of info to return to be passed as array or comma separated stringReturns:An array containing info on the specified file or FALSE on failureReturn type: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 whatinformation you want returned.

Valid $returnedvalues options are: _name, size, date, readable, writeable,executable and fileperms.

  • symbolicpermissions($perms_)

Parameters:

  • $perms (int) – PermissionsReturns:Symbolic permissions stringReturn type:string

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

  1. echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
  • octalpermissions($perms_)

Parameters:

  • $perms (int) – PermissionsReturns:Octal permissions stringReturn type:string

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

  1. echo octal_permissions(fileperms('./index.php')); // 644
  • setrealpath($path[, $checkexistence = FALSE])

Parameters:

  • $path (string) – Path
  • $check_existence (bool) – Whether to check if the path actually existsReturns:An absolute pathReturn type:string

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

Examples:

  1. $file = '/etc/php5/apache2/php.ini';
  2. echo set_realpath($file); // Prints '/etc/php5/apache2/php.ini'
  3.  
  4. $non_existent_file = '/path/to/non-exist-file.txt';
  5. echo set_realpath($non_existent_file, TRUE); // Shows an error, as the path cannot be resolved
  6. echo set_realpath($non_existent_file, FALSE); // Prints '/path/to/non-exist-file.txt'
  7.  
  8. $directory = '/etc/php5';
  9. echo set_realpath($directory); // Prints '/etc/php5/'
  10.  
  11. $non_existent_directory = '/path/to/nowhere';
  12. echo set_realpath($non_existent_directory, TRUE); // Shows an error, as the path cannot be resolved
  13. echo set_realpath($non_existent_directory, FALSE); // Prints '/path/to/nowhere'