Home  >  Article  >  Backend Development  >  Another php FTP upload class

Another php FTP upload class

WBOY
WBOYOriginal
2016-07-25 09:04:33987browse
  1. /**
  2. php ftp upload class
  3. link: bbs.it-home.org
  4. date: 2013/2/25
  5. */
  6. //R FTP processing;
  7. class ftp {
  8. var $ftpUrl = '58.123.24.32';
  9. var $ftpUser = 'test123';
  10. var $ftpPass = 'yourpassword';
  11. var $ftpDir = '/others/';
  12. var $ftpR = ''; //R ftp resource;
  13. var $status = '';
  14. //R 1: Success; 2 :Unable to connect ftp;3: User error;
  15. function ftp() {
  16. if ($this->ftpR = ftp_connect($this->ftpUrl, 21)) {
  17. if (ftp_login($this->ftpR , $this->ftpUser, $this->ftpPass)) {
  18. if (!empty($this->ftpDir)) {
  19. ftp_chdir($this->ftpR, $this->ftpDir);
  20. }
  21. ftp_pasv($this->ftpR, true);//R Enable passive mode;
  22. $this->status = 1;
  23. } else {
  24. $this->status = 3;
  25. }
  26. } else {
  27. $this->status = 2;
  28. }
  29. }
  30. //R Switch directory;
  31. function cd($dir) {
  32. return ftp_chdir($this->ftpR, $dir);
  33. }
  34. / /R Return the current path;
  35. function pwd() {
  36. return ftp_pwd($this->ftpR);
  37. }
  38. //R Upload files;
  39. function put($localFile, $remoteFile = '') {
  40. if ($remoteFile == '') {
  41. $remoteFile = end(explode('/', $localFile));
  42. }
  43. $res = ftp_nb_put($this->ftpR, $remoteFile, $localFile, FTP_BINARY);
  44. while ($res == FTP_MOREDATA) {
  45. $res = ftp_nb_continue($this->ftpR);
  46. }
  47. if ($res == FTP_FINISHED) {
  48. return true;
  49. } elseif ($res == FTP_FAILED) {
  50. return false;
  51. }
  52. }
  53. //R Download file;
  54. function get($remoteFile, $localFile = '') {
  55. if ($localFile == '') {
  56. $localFile = end(explode(' /', $remoteFile));
  57. }
  58. if (ftp_get($this->ftpR, $localFile, $remoteFile, FTP_BINARY)) {
  59. $flag = true;
  60. } else {
  61. $flag = false;
  62. }
  63. return $flag;
  64. }
  65. //R file size;
  66. function size($file) {
  67. return ftp_size($this->ftpR, $file);
  68. }
  69. //R whether the file exists;
  70. function isFile ($file) {
  71. if ($this->size($file) >= 0) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. //R file time
  78. function fileTime( $file) {
  79. return ftp_mdtm($this->ftpR, $file);
  80. }
  81. //R delete file;
  82. function unlink($file) {
  83. return ftp_delete($this->ftpR, $file) ;
  84. }
  85. function nlist($dir = '/service/resource/') {
  86. return ftp_nlist($this->ftpR, $dir);
  87. }
  88. //R Close the connection;
  89. function bye() {
  90. return ftp_close($this->ftpR);
  91. }
  92. }
  93. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn