blob: 7b8bd7a4b96d78a84addeb9ec459dc6057add02a [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
33 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['debug'] = TRUE; $this->ftp->connect($config); $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775); $this->ftp->close();
34
35In this example a list of files is retrieved from the server.
36
37::
38
39 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['debug'] = TRUE; $this->ftp->connect($config); $list = $this->ftp->list_files('/public_html/'); print_r($list); $this->ftp->close();
40
41In this example a local directory is mirrored on the server.
42
43::
44
45 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['debug'] = TRUE; $this->ftp->connect($config); $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/'); $this->ftp->close();
46
47******************
48Function Reference
49******************
50
51$this->ftp->connect()
52=====================
53
54Connects and logs into to the FTP server. Connection preferences are set
55by passing an array to the function, or you can store them in a config
56file.
57
58Here is an example showing how you set preferences manually::
59
60 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['port']     = 21; $config['passive']  = FALSE; $config['debug']    = TRUE; $this->ftp->connect($config);
61
62Setting FTP Preferences in a Config File
63****************************************
64
65If you prefer you can store your FTP preferences in a config file.
66Simply create a new file called the ftp.php, add the $config array in
67that file. Then save the file at config/ftp.php and it will be used
68automatically.
69
70Available connection options
71****************************
72
73- **hostname** - the FTP hostname. Usually something like:
74 ftp.example.com
75- **username** - the FTP username.
76- **password** - the FTP password.
77- **port** - The port number. Set to 21 by default.
78- **debug** - TRUE/FALSE (boolean). Whether to enable debugging to
79 display error messages.
80- **passive** - TRUE/FALSE (boolean). Whether to use passive mode.
81 Passive is set automatically by default.
82
83$this->ftp->upload()
84====================
85
86Uploads a file to your server. You must supply the local path and the
87remote path, and you can optionally set the mode and permissions.
88Example::
89
90 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
91
92**Mode options are:** ascii, binary, and auto (the default). If auto is
93used it will base the mode on the file extension of the source file.
94
95Permissions are available if you are running PHP 5 and can be passed as
96an octal value in the fourth parameter.
97
98$this->ftp->download()
99======================
100
101Downloads a file from your server. You must supply the remote path and
102the local path, and you can optionally set the mode. Example::
103
104 $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
105
106**Mode options are:** ascii, binary, and auto (the default). If auto is
107used it will base the mode on the file extension of the source file.
108
109Returns FALSE if the download does not execute successfully (including
110if PHP does not have permission to write the local file)
111
112$this->ftp->rename()
113====================
114
115Permits you to rename a file. Supply the source file name/path and the
116new file name/path.
117
118::
119
120 // Renames green.html to blue.html $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
121
122$this->ftp->move()
123==================
124
125Lets you move a file. Supply the source and destination paths::
126
127 // Moves blog.html from "joe" to "fred" $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
128
129Note: if the destination file name is different the file will be
130renamed.
131
132$this->ftp->delete_file()
133==========================
134
135Lets you delete a file. Supply the source path with the file name.
136
137::
138
139 $this->ftp->delete_file('/public_html/joe/blog.html');
140
141$this->ftp->delete_dir()
142=========================
143
144Lets you delete a directory and everything it contains. Supply the
145source path to the directory with a trailing slash.
146
147**Important** Be VERY careful with this function. It will recursively
148delete **everything** within the supplied path, including sub-folders
149and all files. Make absolutely sure your path is correct. Try using the
150list_files() function first to verify that your path is correct.
151
152::
153
154 $this->ftp->delete_dir('/public_html/path/to/folder/');
155
156$this->ftp->list_files()
157=========================
158
159Permits you to retrieve a list of files on your server returned as an
160array. You must supply the path to the desired directory.
161
162::
163
164 $list = $this->ftp->list_files('/public_html/'); print_r($list);
165
166$this->ftp->mirror()
167====================
168
169Recursively reads a local folder and everything it contains (including
170sub-folders) and creates a mirror via FTP based on it. Whatever the
171directory structure of the original file path will be recreated on the
172server. You must supply a source path and a destination path::
173
174 $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
175
176$this->ftp->mkdir()
177===================
178
179Lets you create a directory on your server. Supply the path ending in
180the folder name you wish to create, with a trailing slash. Permissions
181can be set by passed an octal value in the second parameter (if you are
182running PHP 5).
183
184::
185
186 // Creates a folder named "bar" $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
187
188$this->ftp->chmod()
189===================
190
191Permits you to set file permissions. Supply the path to the file or
192folder you wish to alter permissions on::
193
194 // Chmod "bar" to 777 $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
195
196$this->ftp->close();
197====================
198
199Closes the connection to your server. It's recommended that you use this
200when you are finished uploading.