summaryrefslogtreecommitdiffstats
path: root/user_guide/libraries/ftp.html
blob: 4df9c545b67f52c78c90b2538003c373d1b2020a (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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<title>Code Igniter 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">
window.onload = function() {
	myHeight = new fx.Height('nav', {duration: 400});
	myHeight.hide();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='Rick Ellis' />
<meta name='description' content='Code Igniter 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.jpg" width="153" height="44" 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>Code Igniter User Guide Version 1.5.0b3</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</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://www.codeigniter.com/">Code Igniter 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="www.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>Code Igniter's FTP Class permits files to be uploaded via FTP to your server.  It also includes a "mirroring" function
that permits an local directory to be recreated remotely via FTP.</p>

<h2>Initializing the Class</h2>

<p>Like most other classes in Code Igniter, 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.  Note: Setting permissions requires PHP 5.</p>

<code>
$this->load->library('ftp');<br />
<br />
$config['hostname'] = 'ftp.your-site.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.your-site.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.your-site.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.your-site.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.some-site.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->sconnect()</h2>

<p>Secure FTP connect.  This function is identical to the function above, except that it initiates a secure connection.</p>


<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 are available if you are running PHP 5 and can be passed as an <kbd>octal</kbd> value in the fourth parameter.</p>


<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:</p>

<code>
// Creates a folder named "bar"<br />
$this->ftp->mkdir('/public_html/foo/bar/');
</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/', 0777);
</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 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->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="uploading.html">File Uploading 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://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
</div>

</body>
</html>