免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2124 | 回复: 1
打印 上一主题 下一主题

从FTP获取自动下载文件的PHP类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-03 18:28 |只看该作者 |倒序浏览
从FTP获取自动下载文件的PHP类






Php代码
  1. 1./**  
  2. 2. * 从FTP获取自动下载文件的PHP类  
  3. 3. * @author yukar ( http://yukar.iteye.com )  
  4. 4. */  
  5. 5.class FtpClass{   
  6. 6.    private $ftpObj;   
  7. 7.    private $ftpHost = "****";    // 服务器地址   
  8. 8.    private $ftpPort = ****;            // 服务器端口   
  9. 9.    private $ftpUser = "****";        // 用户名   
  10. 10.    private $ftpPassword = "****";        // 口令   
  11. 11.    private $localBase = '/your/save/path'; //你存放的目录   
  12. 12.    function __construct($initData=array()){   
  13. 13.        if (isset($initData['ftpHost']) && $initData['ftpHost']){   
  14. 14.            $this->ftpHost = $initData['ftpHost'];   
  15. 15.        }   
  16. 16.        if (isset($initData['ftpPort']) && $initData['ftpPort']){   
  17. 17.            $this->ftpPort = $initData['ftpPort'];   
  18. 18.        }   
  19. 19.        if (isset($initData['ftpUser']) && $initData['ftpUser']){   
  20. 20.            $this->ftpUser = $initData['ftpUser'];   
  21. 21.        }   
  22. 22.        if (isset($initData['ftpPassword']) && $initData['ftpPassword']){   
  23. 23.            $this->ftpPassword = $initData['ftpPassword'];   
  24. 24.        }   
  25. 25.        if (isset($initData['localBase']) && $initData['localBase']){   
  26. 26.            $this->localBase = $initData['localBase'];   
  27. 27.        }   
  28. 28.    }   
  29. 29.      
  30. 30.    function ftp_connect(){   
  31. 31.        if(!$this->ftpObj){   
  32. 32.            $this->ftpObj = ftp_connect($this->ftpHost,$this->ftpPort);    // 连接ftp服务器   
  33. 33.            if($this->ftpObj) {   
  34. 34.                if(ftp_login($this->ftpObj, $this->ftpUser, $this->ftpPassword)) {    // 登录   
  35. 35.                    return true;   
  36. 36.                }else{   
  37. 37.                    return false;   
  38. 38.                }   
  39. 39.            }else {   
  40. 40.                return false;   
  41. 41.            }   
  42. 42.        }   
  43. 43.    }   
  44. 44.      
  45. 45.    function ftp_download_file($fileName) {   
  46. 46.        //获取FTP路径   
  47. 47.        $ftpPath = dirname($fileName) . "/";     
  48. 48.        //获取文件名     
  49. 49.        $selectFile = basename($fileName);      
  50. 50.        // 进入指定路径   
  51. 51.        if(@ftp_chdir($this->ftpObj,$ftpPath)) {               
  52. 52.            //$localBase 如果不存在,新创建目录,务必确保 有创建权限   
  53. 53.            if(!is_dir($this->localBase)){   
  54. 54.                mkdir($this->localBase, 0777);   
  55. 55.            }   
  56. 56.            $toFile = $this->localBase.$fileName;   
  57. 57.            if(ftp_get($this->ftpObj, $toFile, $selectFile, FTP_BINARY)) {    //下载指定的FTP文件到指定的本地文件   
  58. 58.                return true;   
  59. 59.            }else {   
  60. 60.                return false;   
  61. 61.            }   
  62. 62.        }else {   
  63. 63.            return false;   
  64. 64.        }   
  65. 65.    }   
  66. 66.      
  67. 67.    function ftp_quit(){   
  68. 68.        if($this->ftpObj){   
  69. 69.            ftp_quit($this->ftpObj);   
  70. 70.        }   
  71. 71.    }   
  72. 72.  
  73. 73.}  
  74. /**
  75. * 从FTP获取自动下载文件的PHP类
  76. * @author yukar ( http://yukar.iteye.com )
  77. */
  78. class FtpClass{
  79.     private $ftpObj;
  80.     private $ftpHost = "****";    // 服务器地址
  81.     private $ftpPort = ****;            // 服务器端口
  82.     private $ftpUser = "****";        // 用户名
  83.     private $ftpPassword = "****";        // 口令
  84.     private $localBase = '/your/save/path'; //你存放的目录
  85.     function __construct($initData=array()){
  86.         if (isset($initData['ftpHost']) && $initData['ftpHost']){
  87.             $this->ftpHost = $initData['ftpHost'];
  88.         }
  89.         if (isset($initData['ftpPort']) && $initData['ftpPort']){
  90.             $this->ftpPort = $initData['ftpPort'];
  91.         }
  92.         if (isset($initData['ftpUser']) && $initData['ftpUser']){
  93.             $this->ftpUser = $initData['ftpUser'];
  94.         }
  95.         if (isset($initData['ftpPassword']) && $initData['ftpPassword']){
  96.             $this->ftpPassword = $initData['ftpPassword'];
  97.         }
  98.         if (isset($initData['localBase']) && $initData['localBase']){
  99.             $this->localBase = $initData['localBase'];
  100.         }
  101.     }
  102.    
  103.     function ftp_connect(){
  104.         if(!$this->ftpObj){
  105.             $this->ftpObj = ftp_connect($this->ftpHost,$this->ftpPort);    // 连接ftp服务器
  106.             if($this->ftpObj) {
  107.                 if(ftp_login($this->ftpObj, $this->ftpUser, $this->ftpPassword)) {    // 登录
  108.                     return true;
  109.                 }else{
  110.                     return false;
  111.                 }
  112.             }else {
  113.                 return false;
  114.             }
  115.         }
  116.     }
  117.    
  118.     function ftp_download_file($fileName) {
  119.         //获取FTP路径
  120.         $ftpPath = dirname($fileName) . "/";  
  121.         //获取文件名  
  122.         $selectFile = basename($fileName);   
  123.         // 进入指定路径
  124.         if(@ftp_chdir($this->ftpObj,$ftpPath)) {            
  125.             //$localBase 如果不存在,新创建目录,务必确保 有创建权限
  126.             if(!is_dir($this->localBase)){
  127.                 mkdir($this->localBase, 0777);
  128.             }
  129.             $toFile = $this->localBase.$fileName;
  130.             if(ftp_get($this->ftpObj, $toFile, $selectFile, FTP_BINARY)) {    //下载指定的FTP文件到指定的本地文件
  131.                 return true;
  132.             }else {
  133.                 return false;
  134.             }
  135.         }else {
  136.             return false;
  137.         }
  138.     }
  139.    
  140.     function ftp_quit(){
  141.         if($this->ftpObj){
  142.             ftp_quit($this->ftpObj);
  143.         }
  144.     }

  145. }
复制代码
调用就更简单了

Php代码
  1. 1.$ftpObj = new FtpClass();   
  2. 2.$ftpObj->ftp_connect();   
  3. 3.$filename = '/config/config.sample.php';   
  4. 4.$ftpObj->ftp_download_file($filename);   
  5. 5.$ftpObj->ftp_quit();   
  6. 6.exit();  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-03 18:29 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP