blob: 60c5aa98cabc475b7272d201b3efa0df28b5c3db [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
12This helper is loaded using the following code
13
14::
15
16 $this->load->helper('file');
17
18The following functions are available:
19
20read_file('path')
21=================
22
23Returns the data contained in the file specified in the path. Example
24
25::
26
27 $string = read_file('./path/to/file.php');
28
29The path can be a relative or full server path. Returns FALSE (boolean) on failure.
30
31.. note:: The path is relative to your main site index.php file, NOT your
32 controller or view files. CodeIgniter uses a front controller so paths
33 are always relative to the main site index.
34
Andrey Andreev0f0b7692012-06-07 14:57:04 +030035.. note:: This function is DEPRECATED. Use the native ``file_get_contents()``
36 instead.
37
Derek Jones8ede1a22011-10-05 13:34:52 -050038If your server is running an `open_basedir` restriction this function might not work if you are trying to access a file above the calling script.
39
40write_file('path', $data)
41=========================
42
43Writes data to the file specified in the path. If the file does not exist the function will create it. Example
44
45::
46
47 $data = 'Some file data';
48 if ( ! write_file('./path/to/file.php', $data))
49 {     
50 echo 'Unable to write the file';
51 }
52 else
53 {     
54 echo 'File written!';
55 }
56
57You can optionally set the write mode via the third parameter
58
59::
60
61 write_file('./path/to/file.php', $data, 'r+');
62
63The default mode is wb. Please see the `PHP user guide <http://php.net/fopen>`_ for mode options.
64
65Note: In order for this function to write data to a file its file permissions must be set such that it is writable (666, 777, etc.). If the file does not already exist, the directory containing it must be writable.
66
67.. note:: The path is relative to your main site index.php file, NOT your
68 controller or view files. CodeIgniter uses a front controller so paths
69 are always relative to the main site index.
70
71delete_files('path')
72====================
73
74Deletes ALL files contained in the supplied path. Example
75
76::
77
78 delete_files('./path/to/directory/');
79
80If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well. Example
81
82::
83
84 delete_files('./path/to/directory/', TRUE);
85
86.. note:: The files must be writable or owned by the system in order to be deleted.
87
88get_filenames('path/to/directory/')
89===================================
90
91Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.
92
93get_dir_file_info('path/to/directory/', $top_level_only = TRUE)
94===============================================================
95
96Reads the specified directory and builds an array containing the filenames, filesize, dates, and permissions. Sub-folders contained within the specified path are only read if forced by sending the second parameter, $top_level_only to FALSE, as this can be an intensive operation.
97
98get_file_info('path/to/file', $file_information)
99================================================
100
101Given a file and path, returns the name, path, size, date modified. Second parameter allows you to explicitly declare what information you want returned; options are: `name`, `server_path`, `size`, `date`, `readable`, `writable`, `executable`, `fileperms`. Returns FALSE if the file cannot be found.
102
103.. note:: The "writable" uses the PHP function is_writable() which is known
104 to have issues on the IIS webserver. Consider using fileperms instead,
105 which returns information from PHP's fileperms() function.
106
107get_mime_by_extension('file')
108=============================
109
110Translates a file extension into a mime type based on config/mimes.php. Returns FALSE if it can't determine the type, or open the mime config file.
111
112::
113
114 $file = "somefile.png";
115 echo $file . ' is has a mime type of ' . get_mime_by_extension($file);
116
117
118.. note:: This is not an accurate way of determining file mime types, and
119 is here strictly as a convenience. It should not be used for security.
120
121symbolic_permissions($perms)
122============================
123
124Takes numeric permissions (such as is returned by `fileperms()` and returns standard symbolic notation of file permissions.
125
126::
127
128 echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
129
130octal_permissions($perms)
131=========================
132
133Takes numeric permissions (such as is returned by fileperms() and returns a three character octal notation of file permissions.
134
135::
136
137 echo octal_permissions(fileperms('./index.php')); // 644
138