summaryrefslogtreecommitdiffstats
path: root/user_guide/libraries/ftp.html
blob: ea0a328101cb5f3b63554e3a45bd9b2452e24fa4 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FTP Class : CodeIgniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<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>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 2.1.1</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
FTP Class
</td>
<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>
</tr>
</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>FTP Class</h1>

<p>CodeIgniter's FTP Class permits files to be transfered to a remote server. Remote files can also be moved, renamed,
and deleted.  The FTP class also includes a "mirroring" function that permits an entire local directory to be recreated remotely via FTP.</p>

<p class="important"><strong>Note:</strong>&nbsp; SFTP and SSL FTP protocols are not supported, only standard FTP.</p>

<h2>Initializing the Class</h2>

<p>Like most other classes in CodeIgniter, the FTP class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>

<code>$this->load->library('ftp');</code>
<p>Once loaded, the FTP object will be available using: <dfn>$this->ftp</dfn></p>


<h2>Usage Examples</h2>

<p>In this example a connection is opened to the FTP server, and a local file is read and uploaded in ASCII mode. The
file permissions are set to 755.</p>

<code>
$this->load->library('ftp');<br />
<br />
$config['hostname'] = 'ftp.example.com';<br />
$config['username'] = 'your-username';<br />
$config['password'] = 'your-password';<br />
$config['debug']	= TRUE;<br />
<br />
$this->ftp->connect($config);<br />
<br />
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);<br />
<br />
$this->ftp->close();

</code>


<p>In this example a list of files is retrieved from the server.</p>

<code>
$this->load->library('ftp');<br />
<br />
$config['hostname'] = 'ftp.example.com';<br />
$config['username'] = 'your-username';<br />
$config['password'] = 'your-password';<br />
$config['debug']	= TRUE;<br />
<br />
$this->ftp->connect($config);<br />
<br />
$list = $this->ftp->list_files('/public_html/');<br />
<br />
print_r($list);<br />
<br />
$this->ftp->close();
</code>

<p>In this example a local directory is mirrored on the server.</p>


<code>
$this->load->library('ftp');<br />
<br />
$config['hostname'] = 'ftp.example.com';<br />
$config['username'] = 'your-username';<br />
$config['password'] = 'your-password';<br />
$config['debug']	= TRUE;<br />
<br />
$this->ftp->connect($config);<br />
<br />
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');<br />
<br />
$this->ftp->close();
</code>


<h1>Function Reference</h1>

<h2>$this->ftp->connect()</h2>

<p>Connects and logs into to the FTP server. Connection preferences are set by passing an array
to the function, or you can store them in a config file.</p>


<p>Here is an example showing how you set preferences manually:</p>

<code>
$this->load->library('ftp');<br />
<br />
$config['hostname'] = 'ftp.example.com';<br />
$config['username'] = 'your-username';<br />
$config['password'] = 'your-password';<br />
$config['port']&nbsp;&nbsp;&nbsp;&nbsp; = 21;<br />
$config['passive']&nbsp;&nbsp;= FALSE;<br />
$config['debug']&nbsp;&nbsp;&nbsp;&nbsp;= TRUE;<br />
<br />
$this->ftp->connect($config);<br />
</code>

<h3>Setting FTP Preferences in a Config File</h3>

<p>If you prefer you can store your FTP preferences in a config file.
Simply create a new file called the <var>ftp.php</var>,  add the <var>$config</var>
array in that file. Then save the file at <var>config/ftp.php</var> and it will be used automatically.</p>

<h3>Available connection options:</h3>


<ul>
<li><strong>hostname</strong> - the FTP hostname.  Usually something like:&nbsp; <dfn>ftp.example.com</dfn></li>
<li><strong>username</strong> - the FTP username.</li>
<li><strong>password</strong> - the FTP password.</li>
<li><strong>port</strong> - The port number. Set to <dfn>21</dfn> by default.</li>
<li><strong>debug</strong> - <kbd>TRUE/FALSE</kbd> (boolean). Whether to enable debugging to display error messages.</li>
<li><strong>passive</strong> - <kbd>TRUE/FALSE</kbd> (boolean). Whether to use passive mode.  Passive is set automatically by default.</li>
</ul>



<h2>$this->ftp->upload()</h2>

<p>Uploads a file to your server.  You must supply the local path and the remote path, and you can optionally set the mode and permissions.
Example:</p>


<code>$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);</code>

<p><strong>Mode options are:</strong>&nbsp; <kbd>ascii</kbd>, <kbd>binary</kbd>, and <kbd>auto</kbd> (the default). If
<kbd>auto</kbd> is used it will base the mode on the file extension of the source file.</p>

<p>Permissions can be passed as an <kbd>octal</kbd> value in the fourth parameter.</p>

<h2>$this->ftp->download()</h2>

<p>Downloads a file from your server.  You must supply the remote path and the local path, and you can optionally set the mode.
Example:</p>

<code>$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');</code>

<p><strong>Mode options are:</strong>&nbsp; <kbd>ascii</kbd>, <kbd>binary</kbd>, and <kbd>auto</kbd> (the default). If
<kbd>auto</kbd> is used it will base the mode on the file extension of the source file.</p>

<p>Returns FALSE if the download does not execute successfully (including if PHP does not have permission to write the local file)</p>


<h2>$this->ftp->rename()</h2>
<p>Permits you to rename a file.  Supply the source file name/path and the new file name/path.</p>

<code>
// Renames green.html to blue.html<br />
$this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
</code>

<h2>$this->ftp->move()</h2>
<p>Lets you move a file.  Supply the source and destination paths:</p>

<code>
// Moves blog.html from "joe" to "fred"<br />
$this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
</code>

<p>Note: if the destination file name is different the file will be renamed.</p>


<h2>$this->ftp->delete_file()</h2>
<p>Lets you delete a file.  Supply the source path with the file name.</p>

<code>
$this->ftp->delete_file('/public_html/joe/blog.html');
</code>


<h2>$this->ftp->delete_dir()</h2>
<p>Lets you delete a directory and everything it contains.  Supply the source path to the directory with a trailing slash.</p>

<p class="important"><strong>Important</strong>&nbsp; Be VERY careful with this function.  It will recursively delete
<b>everything</b> within the supplied path, including sub-folders and all files.  Make absolutely sure your path is correct.
Try using the <kbd>list_files()</kbd> function first to verify that your path is correct.</p>

<code>
$this->ftp->delete_dir('/public_html/path/to/folder/');
</code>



<h2>$this->ftp->list_files()</h2>
<p>Permits you to retrieve a list of files on your server returned as an <dfn>array</dfn>.  You must supply
the path to the desired directory.</p>

<code>
$list = $this->ftp->list_files('/public_html/');<br />
<br />
print_r($list);
</code>


<h2>$this->ftp->mirror()</h2>

<p>Recursively reads a local folder and everything it contains (including sub-folders) and creates a
mirror via FTP based on it.  Whatever the directory structure of the original file path will be recreated on the server.
You must supply a source path and a destination path:</p>

<code>
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
</code>



<h2>$this->ftp->mkdir()</h2>

<p>Lets you create a directory on your server.  Supply the path ending in the folder name you wish to create, with a trailing slash.
Permissions can be set by passed an <kbd>octal</kbd> value in the second parameter.</p>

<code>
// Creates a folder named "bar"<br />
$this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
</code>


<h2>$this->ftp->chmod()</h2>

<p>Permits you to set file permissions.  Supply the path to the file or folder you wish to alter permissions on:</p>

<code>
// Chmod "bar" to 777<br />
$this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
</code>




<h2>$this->ftp->close();</h2>
<p>Closes the connection to your server.  It's recommended that you use this when you are finished uploading.</p>








</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="form_validation.html">Form Validation Class</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="table.html">HTML Table Class</a>
</p>
<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>
</div>

</body>
</html>