blob: c587869dbe85eecc4f1b0dfbc2994e7fe6c7e3ee [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#########
2FTP Class
3#########
4
5CodeIgniter's FTP Class permits files to be transfered to a remote
6server. 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
Derek Jonesb799a0b2013-08-06 15:38:21 -070093.. class:: CI_FTP
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Derek Jonesb799a0b2013-08-06 15:38:21 -070095 .. method:: connect([$config = array()])
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Derek Jonesb799a0b2013-08-06 15:38:21 -070097 :param array $config: Connection values
98 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Derek Jonesb799a0b2013-08-06 15:38:21 -0700100 Connects and logs into to the FTP server. Connection preferences are set
101 by passing an array to the function, or you can store them in a config
102 file.
Derek Jones5b8ebce2011-10-05 16:00:50 -0500103
Derek Jonesb799a0b2013-08-06 15:38:21 -0700104 Here is an example showing how you set preferences manually::
Derek Jones5b8ebce2011-10-05 16:00:50 -0500105
Derek Jonesb799a0b2013-08-06 15:38:21 -0700106 $this->load->library('ftp');
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Derek Jonesb799a0b2013-08-06 15:38:21 -0700108 $config['hostname'] = 'ftp.example.com';
109 $config['username'] = 'your-username';
110 $config['password'] = 'your-password';
111 $config['port'] = 21;
112 $config['passive'] = FALSE;
113 $config['debug'] = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jonesb799a0b2013-08-06 15:38:21 -0700115 $this->ftp->connect($config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jonesb799a0b2013-08-06 15:38:21 -0700117 **Setting FTP Preferences in a Config File**
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jonesb799a0b2013-08-06 15:38:21 -0700119 If you prefer you can store your FTP preferences in a config file.
120 Simply create a new file called the ftp.php, add the $config array in
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200121 that file. Then save the file at *application/config/ftp.php* and it
122 will be used automatically.
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Derek Jonesb799a0b2013-08-06 15:38:21 -0700124 **Available connection options**
125
126 ================== ===================================
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200127 Option Name Description
Derek Jonesb799a0b2013-08-06 15:38:21 -0700128 ================== ===================================
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200129 **hostname** the FTP hostname. Usually something like: ftp.example.com
130 **username** the FTP username
131 **password** the FTP password
132 **port** The port number. Set to 21 by default.
133 **debug** TRUE/FALSE (boolean). Whether to enable debugging to display error messages.
134 **passive** TRUE/FALSE (boolean). Whether to use passive mode. Passive is set automatically by default.
Derek Jonesb799a0b2013-08-06 15:38:21 -0700135 ================== ===================================
136
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200137 .. method:: upload($locpath, $rempath[, $mode = 'auto'[, $permissions = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200139 :param string $locpath: Local file path
140 :param string $rempath: Remote file path
141 :param string $mode: FTP mode, defaults to 'auto' (options are: 'auto', 'binary', 'ascii')
142 :param int $permissions: File permissions (octal)
143 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200145 Uploads a file to your server. You must supply the local path and the
146 remote path, and you can optionally set the mode and permissions.
147 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200149 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200151 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 -0500152
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200153 If set, permissions have to be passed as an octal value.
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200155 .. method:: download($rempath, $locpath[, $mode = 'auto'])
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200157 :param string $rempath: Remote file path
158 :param string $locpath: Local file path
159 :param string $mode: FTP mode, defaults to 'auto' (options are: 'auto', 'binary', 'ascii')
160 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200162 Downloads a file from your server. You must supply the remote path and
163 the local path, and you can optionally set the mode. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200165 $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200167 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 -0500168
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200169 Returns FALSE if the download does not execute successfully (including if PHP does not have permission to write the local file).
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200171 .. method:: rename($old_file, $new_file, $move = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200173 :param string $old_file: Old file name
174 :param string $new_file: New file name
175 :param bool $move: Whether a move is being performed
176 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200178 Permits you to rename a file. Supply the source file name/path and the new file name/path.
179 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200181 // Renames green.html to blue.html
182 $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200184 .. method:: move($old_file, $new_file)
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200186 :param string $old_file: Old file name
187 :param string $new_file: New file name
188 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200190 Lets you move a file. Supply the source and destination paths::
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200192 // Moves blog.html from "joe" to "fred"
193 $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200195 .. note:: If the destination file name is different the file will be renamed.
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200197 .. method:: delete_file($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200199 :param string $filepath: Path to file to delete
200 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500201
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200202 Lets you delete a file. Supply the source path with the file name.
203 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200205 $this->ftp->delete_file('/public_html/joe/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200207 .. method:: delete_dir($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200209 :param string $filepath: Path to directory to delete
210 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200212 Lets you delete a directory and everything it contains. Supply the
213 source path to the directory with a trailing slash.
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200215 .. important:: Be VERY careful with this method!
216 It will recursively delete **everything** within the supplied path,
217 including sub-folders and all files. Make absolutely sure your path
218 is correct. Try using ``list_files()`` first to verify that your path is correct.
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200220 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200222 $this->ftp->delete_dir('/public_html/path/to/folder/');
Derek Jones5b8ebce2011-10-05 16:00:50 -0500223
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200224 .. method:: list_files([$path = '.'])
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200226 :param string $path: Directory path
227 :returns: array or FALSE on failure
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200229 Permits you to retrieve a list of files on your server returned as an
230 array. You must supply the path to the desired directory.
231 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200233 $list = $this->ftp->list_files('/public_html/');
234 print_r($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200236 .. method:: mirror($locpath, $rempath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200238 :param string $locpath: Local path
239 :param string $rempath: Remote path
240 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200242 Recursively reads a local folder and everything it contains (including
243 sub-folders) and creates a mirror via FTP based on it. Whatever the
244 directory structure of the original file path will be recreated on the
245 server. You must supply a source path and a destination path::
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200247 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500248
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200249 .. method:: mkdir($path[, $permissions = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500250
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200251 :param string $path: Path to directory to create
252 :param int $permissions: Permissions (octal)
253 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200255 Lets you create a directory on your server. Supply the path ending in
256 the folder name you wish to create, with a trailing slash.
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200258 Permissions can be set by passing an octal value in the second parameter.
259 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500260
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200261 // Creates a folder named "bar"
262 $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
263
264 .. method:: chmod($path, $perm)
265
266 :param string $path: Path to alter permissions for
267 :param int $perm: Permissions (octal)
268 :returns: bool
269
270 Permits you to set file permissions. Supply the path to the file or
271 directory you wish to alter permissions on::
272
273 // Chmod "bar" to 777
274 $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
275
Andrey Andreeva3229892014-01-03 17:35:52 +0200276 .. method:: changedir($path[, $suppress_debug = FALSE])
277
278 :param string $path: Directory path
279 :param bool $suppress_debug: Whether to turn off debug messages for this command
280 :returns: bool
281
282 Changes the current working directory to the specified path.
283
284 The ``$suppress_debug`` parameter is useful in case you want to use this method
285 as an ``is_dir()`` alternative for FTP.
286
Andrey Andreeveb21ac82014-01-03 17:27:29 +0200287 .. method:: close()
288
289 :returns: bool
290
291 Closes the connection to your server. It's recommended that you use this
292 when you are finished uploading.