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