blob: 833cddea44ffdc6caa7dfd2a18fbbeeac955a4f6 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2File Helper
3###########
4
5The File Helper file contains functions that assist in working with files.
6
Derek Jones46c95472013-07-21 11:03:33 -07007.. contents::
8 :local:
9
10.. raw:: html
11
12 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14Loading this Helper
15===================
16
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020017This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050018
19 $this->load->helper('file');
20
Derek Jones46c95472013-07-21 11:03:33 -070021Available Functions
22===================
23
Derek Jones8ede1a22011-10-05 13:34:52 -050024The following functions are available:
25
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020027.. php:function:: read_file($file)
Derek Jones8ede1a22011-10-05 13:34:52 -050028
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020029 :param string $file: File path
Andrey Andreev3de130c2014-02-07 23:31:49 +020030 :returns: File contents or FALSE on failure
31 :rtype: string
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020032
Derek Jones46c95472013-07-21 11:03:33 -070033 Returns the data contained in the file specified in the path.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020034
Derek Jones46c95472013-07-21 11:03:33 -070035 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Derek Jones46c95472013-07-21 11:03:33 -070037 $string = read_file('./path/to/file.php');
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Derek Jones46c95472013-07-21 11:03:33 -070039 The path can be a relative or full server path. Returns FALSE (boolean) on failure.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones46c95472013-07-21 11:03:33 -070041 .. 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 Jones8ede1a22011-10-05 13:34:52 -050044
Derek Jones46c95472013-07-21 11:03:33 -070045 .. note:: This function is DEPRECATED. Use the native ``file_get_contents()``
46 instead.
Andrey Andreev0f0b7692012-06-07 14:57:04 +030047
Derek Jones46c95472013-07-21 11:03:33 -070048 .. 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 Jones8ede1a22011-10-05 13:34:52 -050051
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020052.. php:function:: write_file($path, $data[, $mode = 'wb'])
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020054 :param string $path: File path
55 :param string $data: Data to write to file
56 :param string $mode: ``fopen()`` mode
Andrey Andreev3de130c2014-02-07 23:31:49 +020057 :returns: TRUE if the write was successful, FALSE in case of an error
58 :rtype: bool
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020059
Derek Jones46c95472013-07-21 11:03:33 -070060 Writes data to the file specified in the path. If the file does not exist then the
61 function will create it.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020062
Derek Jones46c95472013-07-21 11:03:33 -070063 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Derek Jones46c95472013-07-21 11:03:33 -070065 $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 Jones8ede1a22011-10-05 13:34:52 -050074
Derek Jones46c95472013-07-21 11:03:33 -070075 You can optionally set the write mode via the third parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Derek Jones46c95472013-07-21 11:03:33 -070077 write_file('./path/to/file.php', $data, 'r+');
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Master Yodabd2a7e42015-03-25 02:36:31 -070079 The default mode is 'wb'. Please see the `PHP user guide <http://php.net/manual/en/function.fopen.php>`_
Derek Jones46c95472013-07-21 11:03:33 -070080 for mode options.
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Derek Jones46c95472013-07-21 11:03:33 -070082 .. note: In order for this function to write data to a file, its permissions must
Andrey Andreev45965742014-08-27 20:40:11 +030083 be set such that it is writable. If the file does not already exist,
84 then the directory containing it must be writable.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Derek Jones46c95472013-07-21 11:03:33 -070086 .. 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 Jones8ede1a22011-10-05 13:34:52 -050089
Derek Jones46c95472013-07-21 11:03:33 -070090 .. note:: This function acquires an exclusive lock on the file while writing to it.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020092.. php:function:: delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020093
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 Andreev3de130c2014-02-07 23:31:49 +020097 :returns: TRUE on success, FALSE in case of an error
98 :rtype: bool
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020099
Derek Jones46c95472013-07-21 11:03:33 -0700100 Deletes ALL files contained in the supplied path.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200101
Derek Jones46c95472013-07-21 11:03:33 -0700102 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Derek Jones46c95472013-07-21 11:03:33 -0700104 delete_files('./path/to/directory/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Derek Jones46c95472013-07-21 11:03:33 -0700106 If the second parameter is set to TRUE, any directories contained within the supplied
107 root path will be deleted as well.
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jones46c95472013-07-21 11:03:33 -0700109 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Derek Jones46c95472013-07-21 11:03:33 -0700111 delete_files('./path/to/directory/', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Derek Jones46c95472013-07-21 11:03:33 -0700113 .. note:: The files must be writable or owned by the system in order to be deleted.
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200115.. php:function:: get_filenames($source_dir[, $include_path = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200117 :param string $source_dir: Directory path
118 :param bool $include_path: Whether to include the path as part of the filenames
Andrey Andreev3de130c2014-02-07 23:31:49 +0200119 :returns: An array of file names
120 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Derek Jones46c95472013-07-21 11:03:33 -0700122 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 Jones8ede1a22011-10-05 13:34:52 -0500125
Derek Jones46c95472013-07-21 11:03:33 -0700126 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Derek Jones46c95472013-07-21 11:03:33 -0700128 $controllers = get_filenames(APPPATH.'controllers/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200130.. php:function:: get_dir_file_info($source_dir, $top_level_only)
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200132 :param string $source_dir: Directory path
Andrey Andreev3de130c2014-02-07 23:31:49 +0200133 :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 Andreevf6d9a7c2012-11-08 17:27:57 +0200136
Derek Jones46c95472013-07-21 11:03:33 -0700137 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 Andreevf6d9a7c2012-11-08 17:27:57 +0200141
Derek Jones46c95472013-07-21 11:03:33 -0700142 Example::
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200143
Derek Jones46c95472013-07-21 11:03:33 -0700144 $models_info = get_dir_file_info(APPPATH.'models/');
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200145
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200146.. php:function:: get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')])
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200147
148 :param string $file: File path
149 :param array $returned_values: What type of info to return
Andrey Andreev3de130c2014-02-07 23:31:49 +0200150 :returns: An array containing info on the specified file or FALSE on failure
151 :rtype: array
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200152
Derek Jones46c95472013-07-21 11:03:33 -0700153 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 Andreevf6d9a7c2012-11-08 17:27:57 +0200156
Derek Jones46c95472013-07-21 11:03:33 -0700157 Valid ``$returned_values`` options are: `name`, `size`, `date`, `readable`, `writeable`,
158 `executable` and `fileperms`.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200159
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200160.. php:function:: get_mime_by_extension($filename)
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200161
162 :param string $filename: File name
Andrey Andreev3de130c2014-02-07 23:31:49 +0200163 :returns: MIME type string or FALSE on failure
164 :rtype: string
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200165
Derek Jones46c95472013-07-21 11:03:33 -0700166 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 Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones46c95472013-07-21 11:03:33 -0700169 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Derek Jones46c95472013-07-21 11:03:33 -0700171 $file = 'somefile.png';
172 echo $file.' is has a mime type of '.get_mime_by_extension($file);
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Derek Jones46c95472013-07-21 11:03:33 -0700174 .. 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 Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200178.. php:function:: symbolic_permissions($perms)
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200180 :param int $perms: Permissions
Andrey Andreev3de130c2014-02-07 23:31:49 +0200181 :returns: Symbolic permissions string
182 :rtype: string
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200183
Derek Jones46c95472013-07-21 11:03:33 -0700184 Takes numeric permissions (such as is returned by ``fileperms()``) and returns
185 standard symbolic notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
Derek Jones46c95472013-07-21 11:03:33 -0700187 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Derek Jones46c95472013-07-21 11:03:33 -0700189 echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200191.. php:function:: octal_permissions($perms)
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200192
193 :param int $perms: Permissions
Andrey Andreev3de130c2014-02-07 23:31:49 +0200194 :returns: Octal permissions string
195 :rtype: string
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200196
Derek Jones46c95472013-07-21 11:03:33 -0700197 Takes numeric permissions (such as is returned by ``fileperms()``) and returns
198 a three character octal notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
Derek Jones46c95472013-07-21 11:03:33 -0700200 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500201
Derek Jones46c95472013-07-21 11:03:33 -0700202 echo octal_permissions(fileperms('./index.php')); // 644