blob: 05a3fdcc849b942cdad1cf10e35dc8fa80c02c05 [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
vlakoffa36fd632012-06-27 02:46:19 +020029755.
Derek Jones8ede1a22011-10-05 13:34:52 -050030
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
vlakoffa36fd632012-06-27 02:46:19 +0200139If set, permissions have to be passed as an octal value.
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141$this->ftp->download()
142======================
143
144Downloads a file from your server. You must supply the remote path and
145the local path, and you can optionally set the mode. Example::
146
147 $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
148
149**Mode options are:** ascii, binary, and auto (the default). If auto is
150used it will base the mode on the file extension of the source file.
151
152Returns FALSE if the download does not execute successfully (including
153if PHP does not have permission to write the local file)
154
155$this->ftp->rename()
156====================
157
158Permits you to rename a file. Supply the source file name/path and the
159new file name/path.
160
161::
162
Derek Jones5b8ebce2011-10-05 16:00:50 -0500163 // Renames green.html to blue.html
164 $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
166$this->ftp->move()
167==================
168
169Lets you move a file. Supply the source and destination paths::
170
Derek Jones5b8ebce2011-10-05 16:00:50 -0500171 // Moves blog.html from "joe" to "fred"
172 $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
174Note: if the destination file name is different the file will be
175renamed.
176
177$this->ftp->delete_file()
178==========================
179
180Lets you delete a file. Supply the source path with the file name.
181
182::
183
184 $this->ftp->delete_file('/public_html/joe/blog.html');
185
186$this->ftp->delete_dir()
187=========================
188
189Lets you delete a directory and everything it contains. Supply the
190source path to the directory with a trailing slash.
191
192**Important** Be VERY careful with this function. It will recursively
193delete **everything** within the supplied path, including sub-folders
194and all files. Make absolutely sure your path is correct. Try using the
195list_files() function first to verify that your path is correct.
196
197::
198
199 $this->ftp->delete_dir('/public_html/path/to/folder/');
200
201$this->ftp->list_files()
202=========================
203
204Permits you to retrieve a list of files on your server returned as an
205array. You must supply the path to the desired directory.
206
207::
208
Derek Jones5b8ebce2011-10-05 16:00:50 -0500209 $list = $this->ftp->list_files('/public_html/');
210
211 print_r($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213$this->ftp->mirror()
214====================
215
216Recursively reads a local folder and everything it contains (including
217sub-folders) and creates a mirror via FTP based on it. Whatever the
218directory structure of the original file path will be recreated on the
219server. You must supply a source path and a destination path::
220
221 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
222
223$this->ftp->mkdir()
224===================
225
226Lets you create a directory on your server. Supply the path ending in
227the folder name you wish to create, with a trailing slash. Permissions
228can be set by passed an octal value in the second parameter (if you are
229running PHP 5).
230
231::
232
Derek Jones5b8ebce2011-10-05 16:00:50 -0500233 // Creates a folder named "bar"
234 $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
236$this->ftp->chmod()
237===================
238
239Permits you to set file permissions. Supply the path to the file or
240folder you wish to alter permissions on::
241
Derek Jones5b8ebce2011-10-05 16:00:50 -0500242 // Chmod "bar" to 777
243 $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
245$this->ftp->close();
246====================
247
248Closes the connection to your server. It's recommended that you use this
249when you are finished uploading.