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 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 27 | .. 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 |
| 30 | :returns: string or FALSE on failure |
| 31 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 32 | Returns the data contained in the file specified in the path. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 33 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 34 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 36 | $string = read_file('./path/to/file.php'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 38 | 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] | 39 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 40 | .. note:: The path is relative to your main site index.php file, NOT your |
| 41 | controller or view files. CodeIgniter uses a front controller so paths |
| 42 | are always relative to the main site index. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 44 | .. note:: This function is DEPRECATED. Use the native ``file_get_contents()`` |
| 45 | instead. |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 46 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 47 | .. important:: If your server is running an **open_basedir** restriction this |
| 48 | function might not work if you are trying to access a file above the |
| 49 | calling script. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 52 | .. 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 |
| 57 | :returns: bool |
| 58 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 59 | Writes data to the file specified in the path. If the file does not exist then the |
| 60 | function will create it. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 61 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 62 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 64 | $data = 'Some file data'; |
| 65 | if ( ! write_file('./path/to/file.php', $data)) |
| 66 | { |
| 67 | echo 'Unable to write the file'; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | echo 'File written!'; |
| 72 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 74 | You can optionally set the write mode via the third parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 76 | write_file('./path/to/file.php', $data, 'r+'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 78 | The default mode is 'wb'. Please see the `PHP user guide <http://php.net/fopen>`_ |
| 79 | for mode options. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 81 | .. note: In order for this function to write data to a file, its permissions must |
| 82 | be set such that it is writable (666, 777, etc.). If the file does not |
| 83 | already exist, the directory containing it must be writable. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 85 | .. note:: The path is relative to your main site index.php file, NOT your |
| 86 | controller or view files. CodeIgniter uses a front controller so paths |
| 87 | are always relative to the main site index. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 89 | .. 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] | 90 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 92 | .. 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 |
| 97 | :returns: bool |
| 98 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 99 | Deletes ALL files contained in the supplied path. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 100 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 101 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 103 | delete_files('./path/to/directory/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 105 | If the second parameter is set to TRUE, any directories contained within the supplied |
| 106 | root path will be deleted as well. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 108 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 110 | delete_files('./path/to/directory/', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 112 | .. 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] | 113 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 115 | .. 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 |
| 119 | :returns: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 121 | Takes a server path as input and returns an array containing the names of all files |
| 122 | contained within it. The file path can optionally be added to the file names by setting |
| 123 | the second parameter to TRUE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 125 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 127 | $controllers = get_filenames(APPPATH.'controllers/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 130 | .. 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 |
| 133 | :param bool $top_level_only: Whether to look only at the specified directory |
| 134 | (excluding sub-directories) |
| 135 | :returns: array |
| 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 | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 146 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 147 | .. 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] | 148 | |
| 149 | :param string $file: File path |
| 150 | :param array $returned_values: What type of info to return |
| 151 | :returns: array or FALSE on failure |
| 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 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 160 | .. note:: The *writable* attribute is checked via PHP's ``is_writeable()`` function, which |
| 161 | known to have issues on the IIS webserver. Consider using *fileperms* instead, |
| 162 | which returns information from PHP's ``fileperms()`` function. |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 163 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 164 | .. function:: get_mime_by_extension($filename) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 165 | |
| 166 | :param string $filename: File name |
| 167 | :returns: string or FALSE on failure |
| 168 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 169 | Translates a filename extension into a MIME type based on *config/mimes.php*. |
| 170 | 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] | 171 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 172 | :: |
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 | $file = 'somefile.png'; |
| 175 | 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] | 176 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 177 | .. note:: This is not an accurate way of determining file MIME types, and |
| 178 | is here strictly for convenience. It should not be used for security |
| 179 | purposes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 182 | .. function:: symbolic_permissions($perms) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 184 | :param int $perms: Permissions |
| 185 | :returns: string |
| 186 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 187 | Takes numeric permissions (such as is returned by ``fileperms()``) and returns |
| 188 | standard symbolic notation of file permissions. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 189 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 190 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 191 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 192 | echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r-- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 193 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 194 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 195 | .. function:: octal_permissions($perms) |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 196 | |
| 197 | :param int $perms: Permissions |
| 198 | :returns: string |
| 199 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 200 | Takes numeric permissions (such as is returned by ``fileperms()``) and returns |
| 201 | a three character octal notation of file permissions. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 203 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 204 | |
Derek Jones | 46c9547 | 2013-07-21 11:03:33 -0700 | [diff] [blame] | 205 | echo octal_permissions(fileperms('./index.php')); // 644 |