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