欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

又一个php FTP上传类

程序员文章站 2022-05-16 20:39:11
...
  1. /**
  2. php ftp上传类
  3. link:bbs.it-home.org
  4. date:2013/2/25
  5. */
  6. //R FTP 处理;
  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资源;
  13. var $status = '';
  14. //R 1:成功;2:无法连接ftp;3:用户错误;
  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 启用被动模式;
  22. $this->status = 1;
  23. } else {
  24. $this->status = 3;
  25. }
  26. } else {
  27. $this->status = 2;
  28. }
  29. }
  30. //R 切换目录;
  31. function cd($dir) {
  32. return ftp_chdir($this->ftpR, $dir);
  33. }
  34. //R 返回当前路劲;
  35. function pwd() {
  36. return ftp_pwd($this->ftpR);
  37. }
  38. //R 上传文件;
  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 下载文件;
  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 文件大小;
  66. function size($file) {
  67. return ftp_size($this->ftpR, $file);
  68. }
  69. //R 文件是否存在;
  70. function isFile($file) {
  71. if ($this->size($file) >= 0) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. //R 文件时间
  78. function fileTime($file) {
  79. return ftp_mdtm($this->ftpR, $file);
  80. }
  81. //R 删除文件;
  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 关闭连接;
  89. function bye() {
  90. return ftp_close($this->ftpR);
  91. }
  92. }
  93. ?>
复制代码