blob: 4d225d27833829d8aadc387e83b03c89c5148400 [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
Derek Jonesb8c283a2013-07-19 16:02:53 -070027.. 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
30 :returns: string or FALSE on failure
31
Derek Jones46c95472013-07-21 11:03:33 -070032 Returns the data contained in the file specified in the path.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020033
Derek Jones46c95472013-07-21 11:03:33 -070034 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Derek Jones46c95472013-07-21 11:03:33 -070036 $string = read_file('./path/to/file.php');
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Derek Jones46c95472013-07-21 11:03:33 -070038 The path can be a relative or full server path. Returns FALSE (boolean) on failure.
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Derek Jones46c95472013-07-21 11:03:33 -070040 .. 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 Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jones46c95472013-07-21 11:03:33 -070044 .. note:: This function is DEPRECATED. Use the native ``file_get_contents()``
45 instead.
Andrey Andreev0f0b7692012-06-07 14:57:04 +030046
Derek Jones46c95472013-07-21 11:03:33 -070047 .. 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 Jones8ede1a22011-10-05 13:34:52 -050050
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones46c95472013-07-21 11:03:33 -070052.. 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
57 :returns: bool
58
Derek Jones46c95472013-07-21 11:03:33 -070059 Writes data to the file specified in the path. If the file does not exist then the
60 function will create it.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020061
Derek Jones46c95472013-07-21 11:03:33 -070062 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Derek Jones46c95472013-07-21 11:03:33 -070064 $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 Jones8ede1a22011-10-05 13:34:52 -050073
Derek Jones46c95472013-07-21 11:03:33 -070074 You can optionally set the write mode via the third parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Derek Jones46c95472013-07-21 11:03:33 -070076 write_file('./path/to/file.php', $data, 'r+');
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Derek Jones46c95472013-07-21 11:03:33 -070078 The default mode is 'wb'. Please see the `PHP user guide <http://php.net/fopen>`_
79 for mode options.
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Derek Jones46c95472013-07-21 11:03:33 -070081 .. 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 Jones8ede1a22011-10-05 13:34:52 -050084
Derek Jones46c95472013-07-21 11:03:33 -070085 .. 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 Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jones46c95472013-07-21 11:03:33 -070089 .. note:: This function acquires an exclusive lock on the file while writing to it.
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones46c95472013-07-21 11:03:33 -070092.. 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
97 :returns: bool
98
Derek Jones46c95472013-07-21 11:03:33 -070099 Deletes ALL files contained in the supplied path.
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200100
Derek Jones46c95472013-07-21 11:03:33 -0700101 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Derek Jones46c95472013-07-21 11:03:33 -0700103 delete_files('./path/to/directory/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Derek Jones46c95472013-07-21 11:03:33 -0700105 If the second parameter is set to TRUE, any directories contained within the supplied
106 root path will be deleted as well.
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Derek Jones46c95472013-07-21 11:03:33 -0700108 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Derek Jones46c95472013-07-21 11:03:33 -0700110 delete_files('./path/to/directory/', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jones46c95472013-07-21 11:03:33 -0700112 .. note:: The files must be writable or owned by the system in order to be deleted.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jones46c95472013-07-21 11:03:33 -0700115.. 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
119 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Derek Jones46c95472013-07-21 11:03:33 -0700121 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 Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jones46c95472013-07-21 11:03:33 -0700125 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Derek Jones46c95472013-07-21 11:03:33 -0700127 $controllers = get_filenames(APPPATH.'controllers/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Derek Jonesb8c283a2013-07-19 16:02:53 -0700130.. 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
133 :param bool $top_level_only: Whether to look only at the specified directory
134 (excluding sub-directories)
135 :returns: array
136
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 Andreevf6d9a7c2012-11-08 17:27:57 +0200146
Derek Jones46c95472013-07-21 11:03:33 -0700147.. function:: get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')])
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200148
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 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
Derek Jones46c95472013-07-21 11:03:33 -0700160 .. 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 Andreevf6d9a7c2012-11-08 17:27:57 +0200163
Derek Jonesb8c283a2013-07-19 16:02:53 -0700164.. function:: get_mime_by_extension($filename)
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200165
166 :param string $filename: File name
167 :returns: string or FALSE on failure
168
Derek Jones46c95472013-07-21 11:03:33 -0700169 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 Jones8ede1a22011-10-05 13:34:52 -0500171
Derek Jones46c95472013-07-21 11:03:33 -0700172 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Derek Jones46c95472013-07-21 11:03:33 -0700174 $file = 'somefile.png';
175 echo $file.' is has a mime type of '.get_mime_by_extension($file);
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Derek Jones46c95472013-07-21 11:03:33 -0700177 .. 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 Jones8ede1a22011-10-05 13:34:52 -0500180
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Derek Jonesb8c283a2013-07-19 16:02:53 -0700182.. function:: symbolic_permissions($perms)
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200184 :param int $perms: Permissions
185 :returns: string
186
Derek Jones46c95472013-07-21 11:03:33 -0700187 Takes numeric permissions (such as is returned by ``fileperms()``) and returns
188 standard symbolic notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Derek Jones46c95472013-07-21 11:03:33 -0700190 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Derek Jones46c95472013-07-21 11:03:33 -0700192 echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Derek Jonesb8c283a2013-07-19 16:02:53 -0700195.. function:: octal_permissions($perms)
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200196
197 :param int $perms: Permissions
198 :returns: string
199
Derek Jones46c95472013-07-21 11:03:33 -0700200 Takes numeric permissions (such as is returned by ``fileperms()``) and returns
201 a three character octal notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Derek Jones46c95472013-07-21 11:03:33 -0700203 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
Derek Jones46c95472013-07-21 11:03:33 -0700205 echo octal_permissions(fileperms('./index.php')); // 644