summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/helpers/file_helper.rst
blob: e96546a11a17a7c4ddbc0c35a9c32ddd99768e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
###########
File Helper
###########

The File Helper file contains functions that assist in working with files.

.. contents::
  :local:

.. raw:: html

  <div class="custom-index container"></div>

Loading this Helper
===================

This helper is loaded using the following code::

	$this->load->helper('file');

Available Functions
===================

The following functions are available:

.. php:function:: write_file($path, $data[, $mode = 'wb'])

	:param	string	$path: File path
	:param	string	$data: Data to write to file
	:param	string	$mode: ``fopen()`` mode
	:returns:	TRUE if the write was successful, FALSE in case of an error
	:rtype:	bool

	Writes data to the file specified in the path. If the file does not exist then the
	function will create it.

	Example::

		$data = 'Some file data';
		if ( ! write_file('./path/to/file.php', $data))
		{     
			echo 'Unable to write the file';
		}
		else
		{     
			echo 'File written!';
		}

	You can optionally set the write mode via the third parameter::

		write_file('./path/to/file.php', $data, 'r+');

	The default mode is 'wb'. Please see the `PHP user guide <https://secure.php.net/manual/en/function.fopen.php>`_
	for mode options.

	.. note: In order for this function to write data to a file, its permissions must
		be set such that it is writable. If the file does not already exist,
		then the directory containing it must be writable.

	.. note:: The path is relative to your main site index.php file, NOT your
		controller or view files. CodeIgniter uses a front controller so paths
		are always relative to the main site index.

	.. note:: This function acquires an exclusive lock on the file while writing to it.

.. php:function:: delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])

	:param	string	$path: Directory path
	:param	bool	$del_dir: Whether to also delete directories
	:param	bool	$htdocs: Whether to skip deleting .htaccess and index page files
	:returns:	TRUE on success, FALSE in case of an error
	:rtype:	bool

	Deletes ALL files contained in the supplied path.

	Example::

		delete_files('./path/to/directory/');

	If the second parameter is set to TRUE, any directories contained within the supplied
	root path will be deleted as well.

	Example::

		delete_files('./path/to/directory/', TRUE);

	.. note:: The files must be writable or owned by the system in order to be deleted.

.. php:function:: get_filenames($source_dir[, $include_path = FALSE])

	:param	string	$source_dir: Directory path
	:param	bool	$include_path: Whether to include the path as part of the filenames
	:returns:	An array of file names
	:rtype:	array

	Takes 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.

	Example::

		$controllers = get_filenames(APPPATH.'controllers/');

.. php:function:: get_dir_file_info($source_dir, $top_level_only)

	:param	string	$source_dir: Directory path
	:param	bool	$top_level_only: Whether to look only at the specified directory (excluding sub-directories)
	:returns:	An array containing info on the supplied directory's contents
	:rtype:	array

	Reads 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 to FALSE, as this can be an intensive
	operation.

	Example::

		$models_info = get_dir_file_info(APPPATH.'models/');

.. php:function:: get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')])

	:param	string	$file: File path
	:param	array	$returned_values: What type of info to return
	:returns:	An array containing info on the specified file or FALSE on failure
	:rtype:	array

	Given a file and path, returns (optionally) the *name*, *path*, *size* and *date modified*
	information attributes for a file. Second parameter allows you to explicitly declare what
	information you want returned.

	Valid ``$returned_values`` options are: `name`, `size`, `date`, `readable`, `writeable`,
	`executable` and `fileperms`.

.. php:function:: get_mime_by_extension($filename)

	:param	string	$filename: File name
	:returns:	MIME type string or FALSE on failure
	:rtype:	string

	Translates a filename extension into a MIME type based on *config/mimes.php*.
	Returns FALSE if it can't determine the type, or read the MIME config file.

	::

		$file = 'somefile.png';
		echo $file.' is has a mime type of '.get_mime_by_extension($file);

	.. note:: This is not an accurate way of determining file MIME types, and
		is here strictly for convenience. It should not be used for security
		purposes.

.. php:function:: symbolic_permissions($perms)

	:param	int	$perms: Permissions
	:returns:	Symbolic permissions string
	:rtype:	string

	Takes numeric permissions (such as is returned by ``fileperms()``) and returns
	standard symbolic notation of file permissions.

	::

		echo symbolic_permissions(fileperms('./index.php'));  // -rw-r--r--

.. php:function:: octal_permissions($perms)

	:param	int	$perms: Permissions
	:returns:	Octal permissions string
	:rtype:	string

	Takes numeric permissions (such as is returned by ``fileperms()``) and returns
	a three character octal notation of file permissions.

	::

		echo octal_permissions(fileperms('./index.php')); // 644