blob: 0296191ffcd5a2033c31b90641275692e1eb828f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>File Helper : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21</head>
22<body>
23
24<!-- START NAVIGATION -->
25<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
26<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
27<div id="masthead">
28<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
29<tr>
Phil Sturgeon9c63d0b2011-10-27 01:55:44 +010030<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000031<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
32</tr>
33</table>
34</div>
35<!-- END NAVIGATION -->
36
37
38<!-- START BREADCRUMB -->
39<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
40<tr>
41<td id="breadcrumb">
42<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
43<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44File Helper
45</td>
46<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
47</tr>
48</table>
49<!-- END BREADCRUMB -->
50
51<br clear="all" />
52
53
54<!-- START CONTENT -->
55<div id="content">
56
57
58<h1>File Helper</h1>
59
60<p>The File Helper file contains functions that assist in working with files.</p>
61
62
63<h2>Loading this Helper</h2>
64
65<p>This helper is loaded using the following code:</p>
66<code>$this->load->helper('file');</code>
67
68<p>The following functions are available:</p>
69
70<h2>read_file('<var>path</var>')</h2>
71
Derek Jones4b9c6292011-07-01 17:40:48 -050072<p>Returns the data contained in the file specified in the path. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74<code>$string = read_file('./path/to/file.php');</code>
75
Derek Jones4b9c6292011-07-01 17:40:48 -050076<p>The path can be a relative or full server path. Returns FALSE (boolean) on failure.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000077
78<p class="important"><strong>Note:</strong> The path is relative to your main site index.php file, NOT your controller or view files.
79CodeIgniter uses a front controller so paths are always relative to the main site index.</p>
80
81<p>If your server is running an open_basedir restriction this function
82might not work if you are trying to access a file above the calling script.</p>
83
84<h2>write_file('<var>path</var>', <kbd>$data</kbd>)</h2>
85
Derek Jones4b9c6292011-07-01 17:40:48 -050086<p>Writes data to the file specified in the path. If the file does not exist the function will create it. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000087
88<code>
89$data = 'Some file data';<br />
90<br />
91if ( ! write_file('./path/to/file.php', $data))<br />
92{<br />
93&nbsp;&nbsp;&nbsp;&nbsp; echo 'Unable to write the file';<br />
94}<br />
95else<br />
96{<br />
97&nbsp;&nbsp;&nbsp;&nbsp; echo 'File written!';<br />
98}</code>
99
100<p>You can optionally set the write mode via the third parameter:</p>
101
102<code>write_file('./path/to/file.php', $data, <var>'r+'</var>);</code>
103
Derek Jones4b9c6292011-07-01 17:40:48 -0500104<p>The default mode is <kbd>wb</kbd>. Please see the <a href="http://php.net/fopen">PHP user guide</a> for mode options.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106<p>Note: 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.).
107If the file does not already exist, the directory containing it must be writable.</p>
108
109<p class="important"><strong>Note:</strong> The path is relative to your main site index.php file, NOT your controller or view files.
110CodeIgniter uses a front controller so paths are always relative to the main site index.</p>
111
112<h2>delete_files('<var>path</var>')</h2>
113
Derek Jones4b9c6292011-07-01 17:40:48 -0500114<p>Deletes ALL files contained in the supplied path. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000115<code>delete_files('./path/to/directory/');</code>
116
117<p>If the second parameter is set to <kbd>true</kbd>, any directories contained within the supplied root path will be deleted as well. Example:</p>
118
119<code>delete_files('./path/to/directory/', TRUE);</code>
120
121<p class="important"><strong>Note:</strong> The files must be writable or owned by the system in order to be deleted.</p>
122
123<h2>get_filenames('<var>path/to/directory/</var>')</h2>
124
125<p>Takes a server path as input and returns an array containing the names of all files contained within it. The file path
126can optionally be added to the file names by setting the second parameter to TRUE.</p>
127
Derek Jones788b00f2009-11-27 18:00:20 +0000128<h2>get_dir_file_info('<var>path/to/directory/</var>', <kbd>$top_level_only</kbd> = TRUE)</h2>
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
Derek Jones4b9c6292011-07-01 17:40:48 -0500130<p>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
Derek Jones788b00f2009-11-27 18:00:20 +0000131 by sending the second parameter, <kbd>$top_level_only</kbd> to <samp>FALSE</samp>, as this can be an intensive operation.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
133<h2>get_file_info('<var>path/to/file</var>', <kbd>$file_information</kbd>)</h2>
134
Derek Jones4b9c6292011-07-01 17:40:48 -0500135<p>Given 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.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
137<p class="important"><strong>Note:</strong> The &quot;writable&quot; uses the PHP function is_writable() which is known to have issues on the IIS webserver. Consider using fileperms instead, which returns information from PHP's fileperms() function.</p>
138<h2>get_mime_by_extension('<var>file</var>')</h2>
139
140<p>Translates 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.</p>
141<p>
142<code>$file = &quot;somefile.png&quot;;<br />
143echo $file . ' is has a mime type of ' . get_mime_by_extension($file);</code>
144</p>
Derek Jones4b9c6292011-07-01 17:40:48 -0500145<p class="critical"><strong>Note:</strong> This is not an accurate way of determining file mime types, and is here strictly as a convenience. It should not be used for security.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000146
147<h2>symbolic_permissions(<kbd>$perms</kbd>)</h2>
148
149<p>Takes numeric permissions (such as is returned by <kbd>fileperms()</kbd> and returns standard symbolic notation of file permissions.</p>
150
151<code>echo symbolic_permissions(fileperms('./index.php'));<br />
152<br />
153// -rw-r--r--</code>
154
155<h2>octal_permissions(<kbd>$perms</kbd>)</h2>
156
157<p>Takes numeric permissions (such as is returned by <kbd>fileperms()</kbd> and returns a three character octal notation of file permissions.</p>
158
159<code>echo octal_permissions(fileperms('./index.php'));<br />
160<br />
161// 644</code>
162
163</div>
164
165<!-- END CONTENT -->
166
167
168<div id="footer">
169<p>
170Previous Topic:&nbsp;&nbsp;<a href="email_helper.html">Email Helper</a>
171&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
172<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
173<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
174Next Topic:&nbsp;&nbsp;<a href="form_helper.html">Form Helper</a></p>
Derek Jones898949f2011-01-28 07:42:16 -0600175<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000176</div>
177
178</body>
adminb0dd10f2006-08-25 17:25:49 +0000179</html>