Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | File Helper |
| 3 | ########### |
| 4 | |
| 5 | The File Helper file contains functions that assist in working with files. |
| 6 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 7 | .. contents:: |
| 8 | :local: |
| 9 | |
| 10 | .. raw:: html |
| 11 | |
| 12 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | Loading this Helper |
| 15 | =================== |
| 16 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 17 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
| 19 | $this->load->helper('file'); |
| 20 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 21 | Available Functions |
| 22 | =================== |
| 23 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | The following functions are available: |
| 25 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 27 | .. php:function:: read_file($file) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 29 | :param string $file: File path |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 30 | :returns: File contents or FALSE on failure |
| 31 | :rtype: string |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 32 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 33 | Returns the data contained in the file specified in the path. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 34 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 35 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 37 | $string = read_file('./path/to/file.php'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 39 | The path can be a relative or full server path. Returns FALSE (boolean) on failure. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 41 | .. note:: The path is relative to your main site index.php file, NOT your |
| 42 | controller or view files. CodeIgniter uses a front controller so paths |
| 43 | are always relative to the main site index. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 45 | .. note:: This function is DEPRECATED. Use the native ``file_get_contents()`` |
| 46 | instead. |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 47 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 48 | .. important:: If your server is running an **open_basedir** restriction this |
| 49 | function might not work if you are trying to access a file above the |
| 50 | calling script. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 52 | .. php:function:: write_file($path, $data[, $mode = 'wb']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 54 | :param string $path: File path |
| 55 | :param string $data: Data to write to file |
| 56 | :param string $mode: ``fopen()`` mode |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 57 | :returns: TRUE if the write was successful, FALSE in case of an error |
| 58 | :rtype: bool |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 59 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 60 | Writes data to the file specified in the path. If the file does not exist then the |
| 61 | function will create it. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 62 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 63 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 65 | $data = 'Some file data'; |
| 66 | if ( ! write_file('./path/to/file.php', $data)) |
| 67 | { |
| 68 | echo 'Unable to write the file'; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | echo 'File written!'; |
| 73 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 75 | You can optionally set the write mode via the third parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 77 | write_file('./path/to/file.php', $data, 'r+'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 79 | The default mode is 'wb'. Please see the `PHP user guide <http://php.net/manual/en/function.fopen.php>`_ |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 80 | for mode options. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 82 | .. note: In order for this function to write data to a file, its permissions must |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 83 | be set such that it is writable. If the file does not already exist, |
| 84 | then the directory containing it must be writable. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 86 | .. note:: The path is relative to your main site index.php file, NOT your |
| 87 | controller or view files. CodeIgniter uses a front controller so paths |
| 88 | are always relative to the main site index. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 90 | .. note:: This function acquires an exclusive lock on the file while writing to it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 92 | .. php:function:: delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]]) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 93 | |
| 94 | :param string $path: Directory path |
| 95 | :param bool $del_dir: Whether to also delete directories |
| 96 | :param bool $htdocs: Whether to skip deleting .htaccess and index page files |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 97 | :returns: TRUE on success, FALSE in case of an error |
| 98 | :rtype: bool |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 99 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 100 | Deletes ALL files contained in the supplied path. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 101 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 102 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 104 | delete_files('./path/to/directory/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 106 | If the second parameter is set to TRUE, any directories contained within the supplied |
| 107 | root path will be deleted as well. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 109 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 111 | delete_files('./path/to/directory/', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 113 | .. note:: The files must be writable or owned by the system in order to be deleted. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 115 | .. php:function:: get_filenames($source_dir[, $include_path = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 117 | :param string $source_dir: Directory path |
| 118 | :param bool $include_path: Whether to include the path as part of the filenames |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 119 | :returns: An array of file names |
| 120 | :rtype: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 122 | Takes a server path as input and returns an array containing the names of all files |
| 123 | contained within it. The file path can optionally be added to the file names by setting |
| 124 | the second parameter to TRUE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 126 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 128 | $controllers = get_filenames(APPPATH.'controllers/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 130 | .. php:function:: get_dir_file_info($source_dir, $top_level_only) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 132 | :param string $source_dir: Directory path |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 133 | :param bool $top_level_only: Whether to look only at the specified directory (excluding sub-directories) |
| 134 | :returns: An array containing info on the supplied directory's contents |
| 135 | :rtype: array |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 136 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 137 | Reads the specified directory and builds an array containing the filenames, filesize, |
| 138 | dates, and permissions. Sub-folders contained within the specified path are only read |
| 139 | if forced by sending the second parameter to FALSE, as this can be an intensive |
| 140 | operation. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 141 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 142 | Example:: |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 143 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 144 | $models_info = get_dir_file_info(APPPATH.'models/'); |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 145 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 146 | .. php:function:: get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')]) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 147 | |
| 148 | :param string $file: File path |
| 149 | :param array $returned_values: What type of info to return |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 150 | :returns: An array containing info on the specified file or FALSE on failure |
| 151 | :rtype: array |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 152 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 153 | Given a file and path, returns (optionally) the *name*, *path*, *size* and *date modified* |
| 154 | information attributes for a file. Second parameter allows you to explicitly declare what |
| 155 | information you want returned. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 156 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 157 | Valid ``$returned_values`` options are: `name`, `size`, `date`, `readable`, `writeable`, |
| 158 | `executable` and `fileperms`. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 159 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 160 | .. php:function:: get_mime_by_extension($filename) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 161 | |
| 162 | :param string $filename: File name |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 163 | :returns: MIME type string or FALSE on failure |
| 164 | :rtype: string |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 165 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 166 | Translates a filename extension into a MIME type based on *config/mimes.php*. |
| 167 | Returns FALSE if it can't determine the type, or read the MIME config file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 169 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 171 | $file = 'somefile.png'; |
| 172 | echo $file.' is has a mime type of '.get_mime_by_extension($file); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 174 | .. note:: This is not an accurate way of determining file MIME types, and |
| 175 | is here strictly for convenience. It should not be used for security |
| 176 | purposes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 178 | .. php:function:: symbolic_permissions($perms) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 180 | :param int $perms: Permissions |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 181 | :returns: Symbolic permissions string |
| 182 | :rtype: string |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 183 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 184 | Takes numeric permissions (such as is returned by ``fileperms()``) and returns |
| 185 | standard symbolic notation of file permissions. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 187 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 189 | echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r-- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 191 | .. php:function:: octal_permissions($perms) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 192 | |
| 193 | :param int $perms: Permissions |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 194 | :returns: Octal permissions string |
| 195 | :rtype: string |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 196 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 197 | Takes numeric permissions (such as is returned by ``fileperms()``) and returns |
| 198 | a three character octal notation of file permissions. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 199 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 200 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 201 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 202 | echo octal_permissions(fileperms('./index.php')); // 644 |