blob: 2a015256d4dcfa382b8868d60cc385d0469c5576 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#########
2FTP Class
3#########
4
Andrey Andreev71d8f722017-01-17 12:01:00 +02005CodeIgniter's FTP Class permits files to be transferred to a remote
Derek Jones8ede1a22011-10-05 13:34:52 -05006server. Remote files can also be moved, renamed, and deleted. The FTP
7class also includes a "mirroring" function that permits an entire local
8directory to be recreated remotely via FTP.
9
10.. note:: SFTP and SSL FTP protocols are not supported, only standard
11 FTP.
12
Derek Jonesb799a0b2013-08-06 15:38:21 -070013.. contents::
14 :local:
15
16.. raw:: html
17
18 <div class="custom-index container"></div>
19
20**************************
21Working with the FTP Class
22**************************
23
Derek Jones8ede1a22011-10-05 13:34:52 -050024Initializing the Class
Derek Jonesb799a0b2013-08-06 15:38:21 -070025======================
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27Like most other classes in CodeIgniter, the FTP class is initialized in
28your controller using the $this->load->library function::
29
30 $this->load->library('ftp');
31
32Once loaded, the FTP object will be available using: $this->ftp
33
34Usage Examples
35==============
36
37In this example a connection is opened to the FTP server, and a local
38file is read and uploaded in ASCII mode. The file permissions are set to
vlakoffa36fd632012-06-27 02:46:19 +020039755.
Derek Jones8ede1a22011-10-05 13:34:52 -050040::
41
Derek Jones5b8ebce2011-10-05 16:00:50 -050042 $this->load->library('ftp');
43
44 $config['hostname'] = 'ftp.example.com';
45 $config['username'] = 'your-username';
46 $config['password'] = 'your-password';
47 $config['debug'] = TRUE;
48
49 $this->ftp->connect($config);
50
51 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
52
53 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55In this example a list of files is retrieved from the server.
Derek Jones8ede1a22011-10-05 13:34:52 -050056::
57
Derek Jones5b8ebce2011-10-05 16:00:50 -050058 $this->load->library('ftp');
59
60 $config['hostname'] = 'ftp.example.com';
61 $config['username'] = 'your-username';
62 $config['password'] = 'your-password';
63 $config['debug'] = TRUE;
64
65 $this->ftp->connect($config);
66
67 $list = $this->ftp->list_files('/public_html/');
68
69 print_r($list);
70
71 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73In this example a local directory is mirrored on the server.
Derek Jones8ede1a22011-10-05 13:34:52 -050074::
75
Derek Jones5b8ebce2011-10-05 16:00:50 -050076 $this->load->library('ftp');
77
78 $config['hostname'] = 'ftp.example.com';
79 $config['username'] = 'your-username';
80 $config['password'] = 'your-password';
81 $config['debug'] = TRUE;
82
83 $this->ftp->connect($config);
84
85 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
86
87 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jonesb799a0b2013-08-06 15:38:21 -070089***************
90Class Reference
91***************
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020093.. php:class:: CI_FTP
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020095 .. php:method:: connect([$config = array()])
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreev28c2c972014-02-08 04:27:48 +020097 :param array $config: Connection values
98 :returns: TRUE on success, FALSE on failure
99 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Derek Jonesb799a0b2013-08-06 15:38:21 -0700101 Connects and logs into to the FTP server. Connection preferences are set
102 by passing an array to the function, or you can store them in a config
103 file.
Derek Jones5b8ebce2011-10-05 16:00:50 -0500104
Derek Jonesb799a0b2013-08-06 15:38:21 -0700105 Here is an example showing how you set preferences manually::
Derek Jones5b8ebce2011-10-05 16:00:50 -0500106
Derek Jonesb799a0b2013-08-06 15:38:21 -0700107 $this->load->library('ftp');
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jonesb799a0b2013-08-06 15:38:21 -0700109 $config['hostname'] = 'ftp.example.com';
110 $config['username'] = 'your-username';
111 $config['password'] = 'your-password';
112 $config['port'] = 21;
113 $config['passive'] = FALSE;
114 $config['debug'] = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Derek Jonesb799a0b2013-08-06 15:38:21 -0700116 $this->ftp->connect($config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Derek Jonesb799a0b2013-08-06 15:38:21 -0700118 **Setting FTP Preferences in a Config File**
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Derek Jonesb799a0b2013-08-06 15:38:21 -0700120 If you prefer you can store your FTP preferences in a config file.
121 Simply create a new file called the ftp.php, add the $config array in
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200122 that file. Then save the file at *application/config/ftp.php* and it
123 will be used automatically.
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jonesb799a0b2013-08-06 15:38:21 -0700125 **Available connection options**
126
Andrey Andreev28c2c972014-02-08 04:27:48 +0200127 ============== =============== =============================================================================
128 Option name Default value Description
129 ============== =============== =============================================================================
130 **hostname** n/a FTP hostname (usually something like: ftp.example.com)
131 **username** n/a FTP username
132 **password** n/a FTP password
133 **port** 21 FTP server port number
134 **debug** FALSE TRUE/FALSE (boolean): Whether to enable debugging to display error messages
135 **passive** TRUE TRUE/FALSE (boolean): Whether to use passive mode
136 ============== =============== =============================================================================
Derek Jonesb799a0b2013-08-06 15:38:21 -0700137
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200138 .. php:method:: upload($locpath, $rempath[, $mode = 'auto'[, $permissions = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev28c2c972014-02-08 04:27:48 +0200140 :param string $locpath: Local file path
141 :param string $rempath: Remote file path
142 :param string $mode: FTP mode, defaults to 'auto' (options are: 'auto', 'binary', 'ascii')
143 :param int $permissions: File permissions (octal)
144 :returns: TRUE on success, FALSE on failure
145 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200147 Uploads a file to your server. You must supply the local path and the
148 remote path, and you can optionally set the mode and permissions.
149 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200151 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200153 If 'auto' mode is used it will base the mode on the file extension of the source file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200155 If set, permissions have to be passed as an octal value.
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200157 .. php:method:: download($rempath, $locpath[, $mode = 'auto'])
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev28c2c972014-02-08 04:27:48 +0200159 :param string $rempath: Remote file path
160 :param string $locpath: Local file path
161 :param string $mode: FTP mode, defaults to 'auto' (options are: 'auto', 'binary', 'ascii')
162 :returns: TRUE on success, FALSE on failure
163 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200165 Downloads a file from your server. You must supply the remote path and
166 the local path, and you can optionally set the mode. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200168 $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200170 If 'auto' mode is used it will base the mode on the file extension of the source file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev28c2c972014-02-08 04:27:48 +0200172 Returns FALSE if the download does not execute successfully
173 (including if PHP does not have permission to write the local file).
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200175 .. php:method:: rename($old_file, $new_file[, $move = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Andrey Andreev28c2c972014-02-08 04:27:48 +0200177 :param string $old_file: Old file name
178 :param string $new_file: New file name
179 :param bool $move: Whether a move is being performed
180 :returns: TRUE on success, FALSE on failure
181 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200183 Permits you to rename a file. Supply the source file name/path and the new file name/path.
184 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200186 // Renames green.html to blue.html
187 $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200189 .. php:method:: move($old_file, $new_file)
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
Andrey Andreev28c2c972014-02-08 04:27:48 +0200191 :param string $old_file: Old file name
192 :param string $new_file: New file name
193 :returns: TRUE on success, FALSE on failure
194 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200196 Lets you move a file. Supply the source and destination paths::
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200198 // Moves blog.html from "joe" to "fred"
199 $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200201 .. note:: If the destination file name is different the file will be renamed.
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200203 .. php:method:: delete_file($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
Andrey Andreev28c2c972014-02-08 04:27:48 +0200205 :param string $filepath: Path to file to delete
206 :returns: TRUE on success, FALSE on failure
207 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200209 Lets you delete a file. Supply the source path with the file name.
210 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200212 $this->ftp->delete_file('/public_html/joe/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200214 .. php:method:: delete_dir($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Andrey Andreev28c2c972014-02-08 04:27:48 +0200216 :param string $filepath: Path to directory to delete
217 :returns: TRUE on success, FALSE on failure
218 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200220 Lets you delete a directory and everything it contains. Supply the
221 source path to the directory with a trailing slash.
Derek Jones8ede1a22011-10-05 13:34:52 -0500222
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200223 .. important:: Be VERY careful with this method!
224 It will recursively delete **everything** within the supplied path,
225 including sub-folders and all files. Make absolutely sure your path
226 is correct. Try using ``list_files()`` first to verify that your path is correct.
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200228 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500229
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200230 $this->ftp->delete_dir('/public_html/path/to/folder/');
Derek Jones5b8ebce2011-10-05 16:00:50 -0500231
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200232 .. php:method:: list_files([$path = '.'])
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
Andrey Andreev28c2c972014-02-08 04:27:48 +0200234 :param string $path: Directory path
235 :returns: An array list of files or FALSE on failure
236 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200238 Permits you to retrieve a list of files on your server returned as an
239 array. You must supply the path to the desired directory.
240 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200242 $list = $this->ftp->list_files('/public_html/');
243 print_r($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200245 .. php:method:: mirror($locpath, $rempath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
Andrey Andreev28c2c972014-02-08 04:27:48 +0200247 :param string $locpath: Local path
248 :param string $rempath: Remote path
249 :returns: TRUE on success, FALSE on failure
250 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200252 Recursively reads a local folder and everything it contains (including
253 sub-folders) and creates a mirror via FTP based on it. Whatever the
254 directory structure of the original file path will be recreated on the
255 server. You must supply a source path and a destination path::
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200257 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500258
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200259 .. php:method:: mkdir($path[, $permissions = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500260
Andrey Andreev28c2c972014-02-08 04:27:48 +0200261 :param string $path: Path to directory to create
262 :param int $permissions: Permissions (octal)
263 :returns: TRUE on success, FALSE on failure
264 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200266 Lets you create a directory on your server. Supply the path ending in
267 the folder name you wish to create, with a trailing slash.
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200269 Permissions can be set by passing an octal value in the second parameter.
270 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200272 // Creates a folder named "bar"
Andrey Andreev45965742014-08-27 20:40:11 +0300273 $this->ftp->mkdir('/public_html/foo/bar/', 0755);
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200274
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200275 .. php:method:: chmod($path, $perm)
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200276
Andrey Andreev28c2c972014-02-08 04:27:48 +0200277 :param string $path: Path to alter permissions for
278 :param int $perm: Permissions (octal)
279 :returns: TRUE on success, FALSE on failure
280 :rtype: bool
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200281
282 Permits you to set file permissions. Supply the path to the file or
283 directory you wish to alter permissions on::
284
Andrey Andreev45965742014-08-27 20:40:11 +0300285 // Chmod "bar" to 755
286 $this->ftp->chmod('/public_html/foo/bar/', 0755);
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200287
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200288 .. php:method:: changedir($path[, $suppress_debug = FALSE])
Andrey Andreeva3229892014-01-03 17:35:52 +0200289
Andrey Andreev28c2c972014-02-08 04:27:48 +0200290 :param string $path: Directory path
291 :param bool $suppress_debug: Whether to turn off debug messages for this command
292 :returns: TRUE on success, FALSE on failure
293 :rtype: bool
Andrey Andreeva3229892014-01-03 17:35:52 +0200294
295 Changes the current working directory to the specified path.
296
297 The ``$suppress_debug`` parameter is useful in case you want to use this method
298 as an ``is_dir()`` alternative for FTP.
299
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200300 .. php:method:: close()
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200301
Andrey Andreev28c2c972014-02-08 04:27:48 +0200302 :returns: TRUE on success, FALSE on failure
303 :rtype: bool
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200304
305 Closes the connection to your server. It's recommended that you use this
306 when you are finished uploading.