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 | |
| 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 | |
| 35 | In 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 | |
| 41 | In 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 | ****************** |
| 48 | Function Reference |
| 49 | ****************** |
| 50 | |
| 51 | $this->ftp->connect() |
| 52 | ===================== |
| 53 | |
| 54 | Connects and logs into to the FTP server. Connection preferences are set |
| 55 | by passing an array to the function, or you can store them in a config |
| 56 | file. |
| 57 | |
| 58 | Here 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 | |
| 62 | Setting FTP Preferences in a Config File |
| 63 | **************************************** |
| 64 | |
| 65 | If you prefer you can store your FTP preferences in a config file. |
| 66 | Simply create a new file called the ftp.php, add the $config array in |
| 67 | that file. Then save the file at config/ftp.php and it will be used |
| 68 | automatically. |
| 69 | |
| 70 | Available 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 | |
| 86 | Uploads a file to your server. You must supply the local path and the |
| 87 | remote path, and you can optionally set the mode and permissions. |
| 88 | Example:: |
| 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 |
| 93 | used it will base the mode on the file extension of the source file. |
| 94 | |
| 95 | Permissions are available if you are running PHP 5 and can be passed as |
| 96 | an octal value in the fourth parameter. |
| 97 | |
| 98 | $this->ftp->download() |
| 99 | ====================== |
| 100 | |
| 101 | Downloads a file from your server. You must supply the remote path and |
| 102 | the 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 |
| 107 | used it will base the mode on the file extension of the source file. |
| 108 | |
| 109 | Returns FALSE if the download does not execute successfully (including |
| 110 | if PHP does not have permission to write the local file) |
| 111 | |
| 112 | $this->ftp->rename() |
| 113 | ==================== |
| 114 | |
| 115 | Permits you to rename a file. Supply the source file name/path and the |
| 116 | new 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 | |
| 125 | Lets 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 | |
| 129 | Note: if the destination file name is different the file will be |
| 130 | renamed. |
| 131 | |
| 132 | $this->ftp->delete_file() |
| 133 | ========================== |
| 134 | |
| 135 | Lets 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 | |
| 144 | Lets you delete a directory and everything it contains. Supply the |
| 145 | source path to the directory with a trailing slash. |
| 146 | |
| 147 | **Important** Be VERY careful with this function. It will recursively |
| 148 | delete **everything** within the supplied path, including sub-folders |
| 149 | and all files. Make absolutely sure your path is correct. Try using the |
| 150 | list_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 | |
| 159 | Permits you to retrieve a list of files on your server returned as an |
| 160 | array. 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 | |
| 169 | Recursively reads a local folder and everything it contains (including |
| 170 | sub-folders) and creates a mirror via FTP based on it. Whatever the |
| 171 | directory structure of the original file path will be recreated on the |
| 172 | server. 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 | |
| 179 | Lets you create a directory on your server. Supply the path ending in |
| 180 | the folder name you wish to create, with a trailing slash. Permissions |
| 181 | can be set by passed an octal value in the second parameter (if you are |
| 182 | running 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 | |
| 191 | Permits you to set file permissions. Supply the path to the file or |
| 192 | folder 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 | |
| 199 | Closes the connection to your server. It's recommended that you use this |
| 200 | when you are finished uploading. |