blob: 194d4348f3fc5a2d4435b253d45466738da9a7f9 [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
7.. contents:: Page Contents
8
9Loading this Helper
10===================
11
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020012This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14 $this->load->helper('file');
15
16The following functions are available:
17
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020018read_file()
19===========
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020021.. php:function:: read_file($file)
Derek Jones8ede1a22011-10-05 13:34:52 -050022
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020023 :param string $file: File path
24 :returns: string or FALSE on failure
25
26Returns the data contained in the file specified in the path.
27
28Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050029
30 $string = read_file('./path/to/file.php');
31
32The 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 Andreev0f0b7692012-06-07 14:57:04 +030038.. note:: This function is DEPRECATED. Use the native ``file_get_contents()``
39 instead.
40
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020041.. 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 Jones8ede1a22011-10-05 13:34:52 -050044
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020045write_file()
46============
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020048.. php:function:: write_file($path, $data, $mode = 'wb')
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020050 :param string $path: File path
51 :param string $data: Data to write to file
52 :param string $mode: ``fopen()`` mode
53 :returns: bool
54
55Writes data to the file specified in the path. If the file does not exist then the
56function will create it.
57
58Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050059
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 Andreevf6d9a7c2012-11-08 17:27:57 +020070You can optionally set the write mode via the third parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -050071
72 write_file('./path/to/file.php', $data, 'r+');
73
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020074The default mode is 'wb'. Please see the `PHP user guide <http://php.net/fopen>`_
75for mode options.
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020077.. 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 Jones8ede1a22011-10-05 13:34:52 -050080
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 Andreevf6d9a7c2012-11-08 17:27:57 +020085.. note:: This function acquires an exclusive lock on the file while writing to it.
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020087delete_files()
88==============
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020090.. 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
97Deletes ALL files contained in the supplied path.
98
99Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101 delete_files('./path/to/directory/');
102
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200103If the second parameter is set to TRUE, any directories contained within the supplied
104root path will be deleted as well.
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200106Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
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 Andreevf6d9a7c2012-11-08 17:27:57 +0200112get_filenames()
113===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +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
119 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200121Takes a server path as input and returns an array containing the names of all files
122contained within it. The file path can optionally be added to the file names by setting
123the second parameter to TRUE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200125Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200127 $controllers = get_filenames(APPPATH.'controllers/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200129get_dir_file_info()
130===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200132.. php:function:: get_dir_file_info($source_dir, $top_level_only)
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200134 :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
139Reads the specified directory and builds an array containing the filenames, filesize,
140dates, and permissions. Sub-folders contained within the specified path are only read
141if forced by sending the second parameter to FALSE, as this can be an intensive
142operation.
143
144Example::
145
146 $models_info = get_dir_file_info(APPPATH.'models/');
147
148get_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
157Given a file and path, returns (optionally) the *name*, *path*, *size* and *date modified*
158information attributes for a file. Second parameter allows you to explicitly declare what
159information you want returned.
160
161Valid ``$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
168get_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
176Translates a filename extension into a MIME type based on *config/mimes.php*.
177Returns FALSE if it can't determine the type, or read the MIME config file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
179::
180
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200181 $file = 'somefile.png';
182 echo $file.' is has a mime type of '.get_mime_by_extension($file);
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200184.. 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 Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200188symbolic_permissions()
189======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200191.. php:function:: symbolic_permissions($perms)
Derek Jones8ede1a22011-10-05 13:34:52 -0500192
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200193 :param int $perms: Permissions
194 :returns: string
195
196Takes numeric permissions (such as is returned by ``fileperms()``) and returns
197standard symbolic notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199::
200
201 echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
202
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200203octal_permissions()
204===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200206.. php:function:: octal_permissions($perms)
207
208 :param int $perms: Permissions
209 :returns: string
210
211Takes numeric permissions (such as is returned by ``fileperms()``) and returns
212a three character octal notation of file permissions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214::
215
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200216 echo octal_permissions(fileperms('./index.php')); // 644