| 1 |
lars |
1 |
<!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>FTP Class : 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 |
|
|
|
22 |
</head>
|
|
|
23 |
<body>
|
|
|
24 |
|
|
|
25 |
<!-- START NAVIGATION -->
|
|
|
26 |
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
|
|
27 |
<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>
|
|
|
28 |
<div id="masthead">
|
|
|
29 |
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
|
|
30 |
<tr>
|
|
|
31 |
<td><h1>CodeIgniter User Guide Version 1.7.1</h1></td>
|
|
|
32 |
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
|
|
33 |
</tr>
|
|
|
34 |
</table>
|
|
|
35 |
</div>
|
|
|
36 |
<!-- END NAVIGATION -->
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
<!-- START BREADCRUMB -->
|
|
|
40 |
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
|
|
41 |
<tr>
|
|
|
42 |
<td id="breadcrumb">
|
|
|
43 |
<a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
|
|
44 |
<a href="../index.html">User Guide Home</a> ›
|
|
|
45 |
FTP Class
|
|
|
46 |
</td>
|
|
|
47 |
<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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
|
|
48 |
</tr>
|
|
|
49 |
</table>
|
|
|
50 |
<!-- END BREADCRUMB -->
|
|
|
51 |
|
|
|
52 |
<br clear="all" />
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
<!-- START CONTENT -->
|
|
|
56 |
<div id="content">
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
<h1>FTP Class</h1>
|
|
|
60 |
|
|
|
61 |
<p>CodeIgniter's FTP Class permits files to be transfered to a remote server. Remote files can also be moved, renamed,
|
|
|
62 |
and deleted. The FTP class also includes a "mirroring" function that permits an entire local directory to be recreated remotely via FTP.</p>
|
|
|
63 |
|
|
|
64 |
<p class="important"><strong>Note:</strong> SFTP and SSL FTP protocols are not supported, only standard FTP.</p>
|
|
|
65 |
|
|
|
66 |
<h2>Initializing the Class</h2>
|
|
|
67 |
|
|
|
68 |
<p>Like most other classes in CodeIgniter, the FTP class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
|
|
|
69 |
|
|
|
70 |
<code>$this->load->library('ftp');</code>
|
|
|
71 |
<p>Once loaded, the FTP object will be available using: <dfn>$this->ftp</dfn></p>
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
<h2>Usage Examples</h2>
|
|
|
75 |
|
|
|
76 |
<p>In this example a connection is opened to the FTP server, and a local file is read and uploaded in ASCII mode. The
|
|
|
77 |
file permissions are set to 755. Note: Setting permissions requires PHP 5.</p>
|
|
|
78 |
|
|
|
79 |
<code>
|
|
|
80 |
$this->load->library('ftp');<br />
|
|
|
81 |
<br />
|
|
|
82 |
$config['hostname'] = 'ftp.example.com';<br />
|
|
|
83 |
$config['username'] = 'your-username';<br />
|
|
|
84 |
$config['password'] = 'your-password';<br />
|
|
|
85 |
$config['debug'] = TRUE;<br />
|
|
|
86 |
<br />
|
|
|
87 |
$this->ftp->connect($config);<br />
|
|
|
88 |
<br />
|
|
|
89 |
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);<br />
|
|
|
90 |
<br />
|
|
|
91 |
$this->ftp->close();
|
|
|
92 |
|
|
|
93 |
</code>
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
<p>In this example a list of files is retrieved from the server.</p>
|
|
|
97 |
|
|
|
98 |
<code>
|
|
|
99 |
$this->load->library('ftp');<br />
|
|
|
100 |
<br />
|
|
|
101 |
$config['hostname'] = 'ftp.example.com';<br />
|
|
|
102 |
$config['username'] = 'your-username';<br />
|
|
|
103 |
$config['password'] = 'your-password';<br />
|
|
|
104 |
$config['debug'] = TRUE;<br />
|
|
|
105 |
<br />
|
|
|
106 |
$this->ftp->connect($config);<br />
|
|
|
107 |
<br />
|
|
|
108 |
$list = $this->ftp->list_files('/public_html/');<br />
|
|
|
109 |
<br />
|
|
|
110 |
print_r($list);<br />
|
|
|
111 |
<br />
|
|
|
112 |
$this->ftp->close();
|
|
|
113 |
</code>
|
|
|
114 |
|
|
|
115 |
<p>In this example a local directory is mirrored on the server.</p>
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
<code>
|
|
|
119 |
$this->load->library('ftp');<br />
|
|
|
120 |
<br />
|
|
|
121 |
$config['hostname'] = 'ftp.example.com';<br />
|
|
|
122 |
$config['username'] = 'your-username';<br />
|
|
|
123 |
$config['password'] = 'your-password';<br />
|
|
|
124 |
$config['debug'] = TRUE;<br />
|
|
|
125 |
<br />
|
|
|
126 |
$this->ftp->connect($config);<br />
|
|
|
127 |
<br />
|
|
|
128 |
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');<br />
|
|
|
129 |
<br />
|
|
|
130 |
$this->ftp->close();
|
|
|
131 |
</code>
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
<h1>Function Reference</h1>
|
|
|
135 |
|
|
|
136 |
<h2>$this->ftp->connect()</h2>
|
|
|
137 |
|
|
|
138 |
<p>Connects and logs into to the FTP server. Connection preferences are set by passing an array
|
|
|
139 |
to the function, or you can store them in a config file.</p>
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
<p>Here is an example showing how you set preferences manually:</p>
|
|
|
143 |
|
|
|
144 |
<code>
|
|
|
145 |
$this->load->library('ftp');<br />
|
|
|
146 |
<br />
|
|
|
147 |
$config['hostname'] = 'ftp.example.com';<br />
|
|
|
148 |
$config['username'] = 'your-username';<br />
|
|
|
149 |
$config['password'] = 'your-password';<br />
|
|
|
150 |
$config['port'] = 21;<br />
|
|
|
151 |
$config['passive'] = FALSE;<br />
|
|
|
152 |
$config['debug'] = TRUE;<br />
|
|
|
153 |
<br />
|
|
|
154 |
$this->ftp->connect($config);<br />
|
|
|
155 |
</code>
|
|
|
156 |
|
|
|
157 |
<h3>Setting FTP Preferences in a Config File</h3>
|
|
|
158 |
|
|
|
159 |
<p>If you prefer you can store your FTP preferences in a config file.
|
|
|
160 |
Simply create a new file called the <var>ftp.php</var>, add the <var>$config</var>
|
|
|
161 |
array in that file. Then save the file at <var>config/ftp.php</var> and it will be used automatically.</p>
|
|
|
162 |
|
|
|
163 |
<h3>Available connection options:</h3>
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
<ul>
|
|
|
167 |
<li><strong>hostname</strong> - the FTP hostname. Usually something like: <dfn>ftp.example.com</dfn></li>
|
|
|
168 |
<li><strong>username</strong> - the FTP username.</li>
|
|
|
169 |
<li><strong>password</strong> - the FTP password.</li>
|
|
|
170 |
<li><strong>port</strong> - The port number. Set to <dfn>21</dfn> by default.</li>
|
|
|
171 |
<li><strong>debug</strong> - <kbd>TRUE/FALSE</kbd> (boolean). Whether to enable debugging to display error messages.</li>
|
|
|
172 |
<li><strong>passive</strong> - <kbd>TRUE/FALSE</kbd> (boolean). Whether to use passive mode. Passive is set automatically by default.</li>
|
|
|
173 |
</ul>
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
<h2>$this->ftp->upload()</h2>
|
|
|
178 |
|
|
|
179 |
<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.
|
|
|
180 |
Example:</p>
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<code>$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);</code>
|
|
|
184 |
|
|
|
185 |
<p><strong>Mode options are:</strong> <kbd>ascii</kbd>, <kbd>binary</kbd>, and <kbd>auto</kbd> (the default). If
|
|
|
186 |
<kbd>auto</kbd> is used it will base the mode on the file extension of the source file.</p>
|
|
|
187 |
|
|
|
188 |
<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>
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
<h2>$this->ftp->rename()</h2>
|
|
|
192 |
<p>Permits you to rename a file. Supply the source file name/path and the new file name/path.</p>
|
|
|
193 |
|
|
|
194 |
<code>
|
|
|
195 |
// Renames green.html to blue.html<br />
|
|
|
196 |
$this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
|
|
|
197 |
</code>
|
|
|
198 |
|
|
|
199 |
<h2>$this->ftp->move()</h2>
|
|
|
200 |
<p>Lets you move a file. Supply the source and destination paths:</p>
|
|
|
201 |
|
|
|
202 |
<code>
|
|
|
203 |
// Moves blog.html from "joe" to "fred"<br />
|
|
|
204 |
$this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
|
|
|
205 |
</code>
|
|
|
206 |
|
|
|
207 |
<p>Note: if the destination file name is different the file will be renamed.</p>
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
<h2>$this->ftp->delete_file()</h2>
|
|
|
211 |
<p>Lets you delete a file. Supply the source path with the file name.</p>
|
|
|
212 |
|
|
|
213 |
<code>
|
|
|
214 |
$this->ftp->delete_file('/public_html/joe/blog.html');
|
|
|
215 |
</code>
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
<h2>$this->ftp->delete_dir()</h2>
|
|
|
219 |
<p>Lets you delete a directory and everything it contains. Supply the source path to the directory with a trailing slash.</p>
|
|
|
220 |
|
|
|
221 |
<p class="important"><strong>Important</strong> Be VERY careful with this function. It will recursively delete
|
|
|
222 |
<b>everything</b> within the supplied path, including sub-folders and all files. Make absolutely sure your path is correct.
|
|
|
223 |
Try using the <kbd>list_files()</kbd> function first to verify that your path is correct.</p>
|
|
|
224 |
|
|
|
225 |
<code>
|
|
|
226 |
$this->ftp->delete_dir('/public_html/path/to/folder/');
|
|
|
227 |
</code>
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
<h2>$this->ftp->list_files()</h2>
|
|
|
232 |
<p>Permits you to retrieve a list of files on your server returned as an <dfn>array</dfn>. You must supply
|
|
|
233 |
the path to the desired directory.</p>
|
|
|
234 |
|
|
|
235 |
<code>
|
|
|
236 |
$list = $this->ftp->list_files('/public_html/');<br />
|
|
|
237 |
<br />
|
|
|
238 |
print_r($list);
|
|
|
239 |
</code>
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
<h2>$this->ftp->mirror()</h2>
|
|
|
243 |
|
|
|
244 |
<p>Recursively reads a local folder and everything it contains (including sub-folders) and creates a
|
|
|
245 |
mirror via FTP based on it. Whatever the directory structure of the original file path will be recreated on the server.
|
|
|
246 |
You must supply a source path and a destination path:</p>
|
|
|
247 |
|
|
|
248 |
<code>
|
|
|
249 |
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
|
|
|
250 |
</code>
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
<h2>$this->ftp->mkdir()</h2>
|
|
|
255 |
|
|
|
256 |
<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.
|
|
|
257 |
Permissions can be set by passed an <kbd>octal</kbd> value in the second parameter (if you are running PHP 5).</p>
|
|
|
258 |
|
|
|
259 |
<code>
|
|
|
260 |
// Creates a folder named "bar"<br />
|
|
|
261 |
$this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
|
|
|
262 |
</code>
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
<h2>$this->ftp->chmod()</h2>
|
|
|
266 |
|
|
|
267 |
<p>Permits you to set file permissions. Supply the path to the file or folder you wish to alter permissions on:</p>
|
|
|
268 |
|
|
|
269 |
<code>
|
|
|
270 |
// Chmod "bar" to 777<br />
|
|
|
271 |
$this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
|
|
|
272 |
</code>
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
<h2>$this->ftp->close();</h2>
|
|
|
278 |
<p>Closes the connection to your server. It's recommended that you use this when you are finished uploading.</p>
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
</div>
|
|
|
288 |
<!-- END CONTENT -->
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
<div id="footer">
|
|
|
292 |
<p>
|
|
|
293 |
Previous Topic: <a href="form_validation.html">Form Validation Class</a>
|
|
|
294 |
·
|
|
|
295 |
<a href="#top">Top of Page</a> ·
|
|
|
296 |
<a href="../index.html">User Guide Home</a> ·
|
|
|
297 |
Next Topic: <a href="table.html">HTML Table Class</a>
|
|
|
298 |
</p>
|
|
|
299 |
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
|
|
300 |
</div>
|
|
|
301 |
|
|
|
302 |
</body>
|
|
|
303 |
</html>
|