/user_guide_src/source/libraries/ftp.rst

https://github.com/dchill42/CodeIgniter · ReStructuredText · 249 lines · 165 code · 84 blank · 0 comment · 0 complexity · ee6919d3584a20b92414253a7d0f57d1 MD5 · raw file

  1. #########
  2. FTP Class
  3. #########
  4. CodeIgniter's FTP Class permits files to be transfered to a remote
  5. server. Remote files can also be moved, renamed, and deleted. The FTP
  6. class also includes a "mirroring" function that permits an entire local
  7. directory to be recreated remotely via FTP.
  8. .. note:: SFTP and SSL FTP protocols are not supported, only standard
  9. FTP.
  10. **********************
  11. Initializing the Class
  12. **********************
  13. Like most other classes in CodeIgniter, the FTP class is initialized in
  14. your controller using the $this->load->library function::
  15. $this->load->library('ftp');
  16. Once loaded, the FTP object will be available using: $this->ftp
  17. Usage Examples
  18. ==============
  19. In this example a connection is opened to the FTP server, and a local
  20. file is read and uploaded in ASCII mode. The file permissions are set to
  21. 755.
  22. ::
  23. $this->load->library('ftp');
  24. $config['hostname'] = 'ftp.example.com';
  25. $config['username'] = 'your-username';
  26. $config['password'] = 'your-password';
  27. $config['debug'] = TRUE;
  28. $this->ftp->connect($config);
  29. $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
  30. $this->ftp->close();
  31. In this example a list of files is retrieved from the server.
  32. ::
  33. $this->load->library('ftp');
  34. $config['hostname'] = 'ftp.example.com';
  35. $config['username'] = 'your-username';
  36. $config['password'] = 'your-password';
  37. $config['debug'] = TRUE;
  38. $this->ftp->connect($config);
  39. $list = $this->ftp->list_files('/public_html/');
  40. print_r($list);
  41. $this->ftp->close();
  42. In this example a local directory is mirrored on the server.
  43. ::
  44. $this->load->library('ftp');
  45. $config['hostname'] = 'ftp.example.com';
  46. $config['username'] = 'your-username';
  47. $config['password'] = 'your-password';
  48. $config['debug'] = TRUE;
  49. $this->ftp->connect($config);
  50. $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
  51. $this->ftp->close();
  52. ******************
  53. Function Reference
  54. ******************
  55. $this->ftp->connect()
  56. =====================
  57. Connects and logs into to the FTP server. Connection preferences are set
  58. by passing an array to the function, or you can store them in a config
  59. file.
  60. Here is an example showing how you set preferences manually::
  61. $this->load->library('ftp');
  62. $config['hostname'] = 'ftp.example.com';
  63. $config['username'] = 'your-username';
  64. $config['password'] = 'your-password';
  65. $config['port'] = 21;
  66. $config['passive'] = FALSE;
  67. $config['debug'] = TRUE;
  68. $this->ftp->connect($config);
  69. Setting FTP Preferences in a Config File
  70. ****************************************
  71. If you prefer you can store your FTP preferences in a config file.
  72. Simply create a new file called the ftp.php, add the $config array in
  73. that file. Then save the file at config/ftp.php and it will be used
  74. automatically.
  75. Available connection options
  76. ****************************
  77. - **hostname** - the FTP hostname. Usually something like:
  78. ftp.example.com
  79. - **username** - the FTP username.
  80. - **password** - the FTP password.
  81. - **port** - The port number. Set to 21 by default.
  82. - **debug** - TRUE/FALSE (boolean). Whether to enable debugging to
  83. display error messages.
  84. - **passive** - TRUE/FALSE (boolean). Whether to use passive mode.
  85. Passive is set automatically by default.
  86. $this->ftp->upload()
  87. ====================
  88. Uploads a file to your server. You must supply the local path and the
  89. remote path, and you can optionally set the mode and permissions.
  90. Example::
  91. $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
  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. If set, permissions have to be passed as an octal value.
  95. $this->ftp->download()
  96. ======================
  97. Downloads a file from your server. You must supply the remote path and
  98. the local path, and you can optionally set the mode. Example::
  99. $this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
  100. **Mode options are:** ascii, binary, and auto (the default). If auto is
  101. used it will base the mode on the file extension of the source file.
  102. Returns FALSE if the download does not execute successfully (including
  103. if PHP does not have permission to write the local file)
  104. $this->ftp->rename()
  105. ====================
  106. Permits you to rename a file. Supply the source file name/path and the
  107. new file name/path.
  108. ::
  109. // Renames green.html to blue.html
  110. $this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
  111. $this->ftp->move()
  112. ==================
  113. Lets you move a file. Supply the source and destination paths::
  114. // Moves blog.html from "joe" to "fred"
  115. $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
  116. Note: if the destination file name is different the file will be
  117. renamed.
  118. $this->ftp->delete_file()
  119. ==========================
  120. Lets you delete a file. Supply the source path with the file name.
  121. ::
  122. $this->ftp->delete_file('/public_html/joe/blog.html');
  123. $this->ftp->delete_dir()
  124. =========================
  125. Lets you delete a directory and everything it contains. Supply the
  126. source path to the directory with a trailing slash.
  127. **Important** Be VERY careful with this function. It will recursively
  128. delete **everything** within the supplied path, including sub-folders
  129. and all files. Make absolutely sure your path is correct. Try using the
  130. list_files() function first to verify that your path is correct.
  131. ::
  132. $this->ftp->delete_dir('/public_html/path/to/folder/');
  133. $this->ftp->list_files()
  134. =========================
  135. Permits you to retrieve a list of files on your server returned as an
  136. array. You must supply the path to the desired directory.
  137. ::
  138. $list = $this->ftp->list_files('/public_html/');
  139. print_r($list);
  140. $this->ftp->mirror()
  141. ====================
  142. Recursively reads a local folder and everything it contains (including
  143. sub-folders) and creates a mirror via FTP based on it. Whatever the
  144. directory structure of the original file path will be recreated on the
  145. server. You must supply a source path and a destination path::
  146. $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
  147. $this->ftp->mkdir()
  148. ===================
  149. Lets you create a directory on your server. Supply the path ending in
  150. the folder name you wish to create, with a trailing slash. Permissions
  151. can be set by passed an octal value in the second parameter (if you are
  152. running PHP 5).
  153. ::
  154. // Creates a folder named "bar"
  155. $this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
  156. $this->ftp->chmod()
  157. ===================
  158. Permits you to set file permissions. Supply the path to the file or
  159. folder you wish to alter permissions on::
  160. // Chmod "bar" to 777
  161. $this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
  162. $this->ftp->close();
  163. ====================
  164. Closes the connection to your server. It's recommended that you use this
  165. when you are finished uploading.