blob: 20b11a5c8629917795b33bb6d041ce256e7ed8b6 [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
13**********************
14Initializing the Class
15**********************
16
17Like most other classes in CodeIgniter, the FTP class is initialized in
18your controller using the $this->load->library function::
19
20 $this->load->library('ftp');
21
22Once loaded, the FTP object will be available using: $this->ftp
23
24Usage Examples
25==============
26
27In this example a connection is opened to the FTP server, and a local
28file is read and uploaded in ASCII mode. The file permissions are set to
29755. Note: Setting permissions requires PHP 5.
30
31::
32
Derek Jones5b8ebce2011-10-05 16:00:50 -050033 $this->load->library('ftp');
34
35 $config['hostname'] = 'ftp.example.com';
36 $config['username'] = 'your-username';
37 $config['password'] = 'your-password';
38 $config['debug'] = TRUE;
39
40 $this->ftp->connect($config);
41
42 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
43
44 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050045
46In this example a list of files is retrieved from the server.
47
48::
49
Derek Jones5b8ebce2011-10-05 16:00:50 -050050 $this->load->library('ftp');
51
52 $config['hostname'] = 'ftp.example.com';
53 $config['username'] = 'your-username';
54 $config['password'] = 'your-password';
55 $config['debug'] = TRUE;
56
57 $this->ftp->connect($config);
58
59 $list = $this->ftp->list_files('/public_html/');
60
61 print_r($list);
62
63 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65In this example a local directory is mirrored on the server.
66
67::
68
Derek Jones5b8ebce2011-10-05 16:00:50 -050069 $this->load->library('ftp');
70
71 $config['hostname'] = 'ftp.example.com';
72 $config['username'] = 'your-username';
73 $config['password'] = 'your-password';
74 $config['debug'] = TRUE;
75
76 $this->ftp->connect($config);
77
78 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
79
80 $this->ftp->close();
Derek Jones8ede1a22011-10-05 13:34:52 -050081
82******************
83Function Reference
84******************
85
86$this->ftp->connect()
87=====================
88
89Connects and logs into to the FTP server. Connection preferences are set
90by passing an array to the function, or you can store them in a config
91file.
92
93Here is an example showing how you set preferences manually::
94
Derek Jones5b8ebce2011-10-05 16:00:50 -050095 $this->load->library('ftp');
96
97 $config['hostname'] = 'ftp.example.com';
98 $config['username'] = 'your-username';
99 $config['password'] = 'your-password';
100 $config['port'] = 21;
101 $config['passive'] = FALSE;
102 $config['debug'] = TRUE;
103
104 $this->ftp->connect($config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
106Setting FTP Preferences in a Config File
107****************************************
108
109If you prefer you can store your FTP preferences in a config file.
110Simply create a new file called the ftp.php, add the $config array in
111that file. Then save the file at config/ftp.php and it will be used
112automatically.
113
114Available connection options
115****************************
116
117- **hostname** - the FTP hostname. Usually something like:
118 ftp.example.com
119- **username** - the FTP username.
120- **password** - the FTP password.
121- **port** - The port number. Set to 21 by default.
122- **debug** - TRUE/FALSE (boolean). Whether to enable debugging to
123 display error messages.
124- **passive** - TRUE/FALSE (boolean). Whether to use passive mode.
125 Passive is set automatically by default.
126
127$this->ftp->upload()
128====================
129
130Uploads a file to your server. You must supply the local path and the
131remote path, and you can optionally set the mode and permissions.
132Example::
133
134 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
135
136**Mode options are:** ascii, binary, and auto (the default). If auto is
137used it will base the mode on the file extension of the source file.
138
139Permissions are available if you are running PHP 5 and can be passed as
140an octal value in the fourth parameter.
141
142$this->ftp->download()
143======================
144
145Downloads a file from your server. You must supply the remote path and
146the local path, and you can optionally set the mode. Example::
147
148 $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
149
150**Mode options are:** ascii, binary, and auto (the default). If auto is
151used it will base the mode on the file extension of the source file.
152
153Returns FALSE if the download does not execute successfully (including
154if PHP does not have permission to write the local file)
155
156$this->ftp->rename()
157====================
158
159Permits you to rename a file. Supply the source file name/path and the
160new file name/path.
161
162::
163
Derek Jones5b8ebce2011-10-05 16:00:50 -0500164 // Renames green.html to blue.html
165 $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
167$this->ftp->move()
168==================
169
170Lets you move a file. Supply the source and destination paths::
171
Derek Jones5b8ebce2011-10-05 16:00:50 -0500172 // Moves blog.html from "joe" to "fred"
173 $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175Note: if the destination file name is different the file will be
176renamed.
177
178$this->ftp->delete_file()
179==========================
180
181Lets you delete a file. Supply the source path with the file name.
182
183::
184
185 $this->ftp->delete_file('/public_html/joe/blog.html');
186
187$this->ftp->delete_dir()
188=========================
189
190Lets you delete a directory and everything it contains. Supply the
191source path to the directory with a trailing slash.
192
193**Important** Be VERY careful with this function. It will recursively
194delete **everything** within the supplied path, including sub-folders
195and all files. Make absolutely sure your path is correct. Try using the
196list_files() function first to verify that your path is correct.
197
198::
199
200 $this->ftp->delete_dir('/public_html/path/to/folder/');
201
202$this->ftp->list_files()
203=========================
204
205Permits you to retrieve a list of files on your server returned as an
206array. You must supply the path to the desired directory.
207
208::
209
Derek Jones5b8ebce2011-10-05 16:00:50 -0500210 $list = $this->ftp->list_files('/public_html/');
211
212 print_r($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214$this->ftp->mirror()
215====================
216
217Recursively reads a local folder and everything it contains (including
218sub-folders) and creates a mirror via FTP based on it. Whatever the
219directory structure of the original file path will be recreated on the
220server. You must supply a source path and a destination path::
221
222 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
223
224$this->ftp->mkdir()
225===================
226
227Lets you create a directory on your server. Supply the path ending in
228the folder name you wish to create, with a trailing slash. Permissions
229can be set by passed an octal value in the second parameter (if you are
230running PHP 5).
231
232::
233
Derek Jones5b8ebce2011-10-05 16:00:50 -0500234 // Creates a folder named "bar"
235 $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500236
237$this->ftp->chmod()
238===================
239
240Permits you to set file permissions. Supply the path to the file or
241folder you wish to alter permissions on::
242
Derek Jones5b8ebce2011-10-05 16:00:50 -0500243 // Chmod "bar" to 777
244 $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
246$this->ftp->close();
247====================
248
249Closes the connection to your server. It's recommended that you use this
250when you are finished uploading.