Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######### |
| 2 | FTP Class |
| 3 | ######### |
| 4 | |
| 5 | CodeIgniter's FTP Class permits files to be transfered to a remote |
| 6 | server. Remote files can also be moved, renamed, and deleted. The FTP |
| 7 | class also includes a "mirroring" function that permits an entire local |
| 8 | directory to be recreated remotely via FTP. |
| 9 | |
| 10 | .. note:: SFTP and SSL FTP protocols are not supported, only standard |
| 11 | FTP. |
| 12 | |
| 13 | ********************** |
| 14 | Initializing the Class |
| 15 | ********************** |
| 16 | |
| 17 | Like most other classes in CodeIgniter, the FTP class is initialized in |
| 18 | your controller using the $this->load->library function:: |
| 19 | |
| 20 | $this->load->library('ftp'); |
| 21 | |
| 22 | Once loaded, the FTP object will be available using: $this->ftp |
| 23 | |
| 24 | Usage Examples |
| 25 | ============== |
| 26 | |
| 27 | In this example a connection is opened to the FTP server, and a local |
| 28 | file is read and uploaded in ASCII mode. The file permissions are set to |
| 29 | 755. Note: Setting permissions requires PHP 5. |
| 30 | |
| 31 | :: |
| 32 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 33 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
| 46 | In this example a list of files is retrieved from the server. |
| 47 | |
| 48 | :: |
| 49 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 50 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
| 65 | In this example a local directory is mirrored on the server. |
| 66 | |
| 67 | :: |
| 68 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 69 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
| 82 | ****************** |
| 83 | Function Reference |
| 84 | ****************** |
| 85 | |
| 86 | $this->ftp->connect() |
| 87 | ===================== |
| 88 | |
| 89 | Connects and logs into to the FTP server. Connection preferences are set |
| 90 | by passing an array to the function, or you can store them in a config |
| 91 | file. |
| 92 | |
| 93 | Here is an example showing how you set preferences manually:: |
| 94 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 95 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
| 106 | Setting FTP Preferences in a Config File |
| 107 | **************************************** |
| 108 | |
| 109 | If you prefer you can store your FTP preferences in a config file. |
| 110 | Simply create a new file called the ftp.php, add the $config array in |
| 111 | that file. Then save the file at config/ftp.php and it will be used |
| 112 | automatically. |
| 113 | |
| 114 | Available 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 | |
| 130 | Uploads a file to your server. You must supply the local path and the |
| 131 | remote path, and you can optionally set the mode and permissions. |
| 132 | Example:: |
| 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 |
| 137 | used it will base the mode on the file extension of the source file. |
| 138 | |
| 139 | Permissions are available if you are running PHP 5 and can be passed as |
| 140 | an octal value in the fourth parameter. |
| 141 | |
| 142 | $this->ftp->download() |
| 143 | ====================== |
| 144 | |
| 145 | Downloads a file from your server. You must supply the remote path and |
| 146 | the 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 |
| 151 | used it will base the mode on the file extension of the source file. |
| 152 | |
| 153 | Returns FALSE if the download does not execute successfully (including |
| 154 | if PHP does not have permission to write the local file) |
| 155 | |
| 156 | $this->ftp->rename() |
| 157 | ==================== |
| 158 | |
| 159 | Permits you to rename a file. Supply the source file name/path and the |
| 160 | new file name/path. |
| 161 | |
| 162 | :: |
| 163 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 164 | // Renames green.html to blue.html |
| 165 | $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
| 167 | $this->ftp->move() |
| 168 | ================== |
| 169 | |
| 170 | Lets you move a file. Supply the source and destination paths:: |
| 171 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 172 | // Moves blog.html from "joe" to "fred" |
| 173 | $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
| 175 | Note: if the destination file name is different the file will be |
| 176 | renamed. |
| 177 | |
| 178 | $this->ftp->delete_file() |
| 179 | ========================== |
| 180 | |
| 181 | Lets 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 | |
| 190 | Lets you delete a directory and everything it contains. Supply the |
| 191 | source path to the directory with a trailing slash. |
| 192 | |
| 193 | **Important** Be VERY careful with this function. It will recursively |
| 194 | delete **everything** within the supplied path, including sub-folders |
| 195 | and all files. Make absolutely sure your path is correct. Try using the |
| 196 | list_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 | |
| 205 | Permits you to retrieve a list of files on your server returned as an |
| 206 | array. You must supply the path to the desired directory. |
| 207 | |
| 208 | :: |
| 209 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 210 | $list = $this->ftp->list_files('/public_html/'); |
| 211 | |
| 212 | print_r($list); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 213 | |
| 214 | $this->ftp->mirror() |
| 215 | ==================== |
| 216 | |
| 217 | Recursively reads a local folder and everything it contains (including |
| 218 | sub-folders) and creates a mirror via FTP based on it. Whatever the |
| 219 | directory structure of the original file path will be recreated on the |
| 220 | server. 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 | |
| 227 | Lets you create a directory on your server. Supply the path ending in |
| 228 | the folder name you wish to create, with a trailing slash. Permissions |
| 229 | can be set by passed an octal value in the second parameter (if you are |
| 230 | running PHP 5). |
| 231 | |
| 232 | :: |
| 233 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 234 | // Creates a folder named "bar" |
| 235 | $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 236 | |
| 237 | $this->ftp->chmod() |
| 238 | =================== |
| 239 | |
| 240 | Permits you to set file permissions. Supply the path to the file or |
| 241 | folder you wish to alter permissions on:: |
| 242 | |
Derek Jones | 5b8ebce | 2011-10-05 16:00:50 -0500 | [diff] [blame] | 243 | // Chmod "bar" to 777 |
| 244 | $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 245 | |
| 246 | $this->ftp->close(); |
| 247 | ==================== |
| 248 | |
| 249 | Closes the connection to your server. It's recommended that you use this |
| 250 | when you are finished uploading. |