{"id":18,"date":"2015-03-09T09:48:56","date_gmt":"2015-03-09T01:48:56","guid":{"rendered":"http:\/\/www.cxybj.com\/?p=18"},"modified":"2016-04-11T15:41:46","modified_gmt":"2016-04-11T07:41:46","slug":"php-ftp%e6%93%8d%e4%bd%9c%e7%b1%bb-%e4%b8%8a%e4%bc%a0%e3%80%81%e6%8b%b7%e8%b4%9d%e3%80%81%e7%a7%bb%e5%8a%a8%e3%80%81%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6%e5%88%9b%e5%bb%ba%e7%9b%ae%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=18","title":{"rendered":"PHP FTP\u64cd\u4f5c\u7c7b( \u4e0a\u4f20\u3001\u62f7\u8d1d\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\/\u521b\u5efa\u76ee\u5f55 )"},"content":{"rendered":"<pre>\/**\r\n* \u4f5c\u7528\uff1aFTP\u64cd\u4f5c\u7c7b( \u62f7\u8d1d\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\/\u521b\u5efa\u76ee\u5f55 )\r\n*\/\r\nclass class_ftp\r\n{\r\n    public $off; \/\/ \u8fd4\u56de\u64cd\u4f5c\u72b6\u6001(\u6210\u529f\/\u5931\u8d25)\r\n    public $conn_id; \/\/ FTP\u8fde\u63a5\r\n    \/**\r\n     * \u65b9\u6cd5\uff1aFTP\u8fde\u63a5\r\n     * @FTP_HOST -- FTP\u4e3b\u673a\r\n     * @FTP_PORT -- \u7aef\u53e3\r\n     * @FTP_USER -- \u7528\u6237\u540d\r\n     * @FTP_PASS -- \u5bc6\u7801\r\n     *\/\r\n    function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)\r\n    {\r\n        $this-&gt;conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die(\"FTP\u670d\u52a1\u5668\u8fde\u63a5\u5931\u8d25\");\r\n        @ftp_login($this-&gt;conn_id,$FTP_USER,$FTP_PASS) or die(\"FTP\u670d\u52a1\u5668\u767b\u9646\u5931\u8d25\");\r\n        @ftp_pasv($this-&gt;conn_id,1); \/\/ \u6253\u5f00\u88ab\u52a8\u6a21\u62df\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u4e0a\u4f20\u6587\u4ef6\r\n     * @path -- \u672c\u5730\u8def\u5f84\r\n     * @newpath -- \u4e0a\u4f20\u8def\u5f84\r\n     * @type -- \u82e5\u76ee\u6807\u76ee\u5f55\u4e0d\u5b58\u5728\u5219\u65b0\u5efa\r\n     *\/\r\n    function up_file($path,$newpath,$type=true)\r\n    {\r\n        if($type) $this-&gt;dir_mkdirs($newpath);\r\n        $this-&gt;off = @ftp_put($this-&gt;conn_id,$newpath,$path,FTP_BINARY);\r\n        if(!$this-&gt;off) echo \"\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6743\u9650\u53ca\u8def\u5f84\u662f\u5426\u6b63\u786e\uff01\";\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u79fb\u52a8\u6587\u4ef6\r\n     * @path -- \u539f\u8def\u5f84\r\n     * @newpath -- \u65b0\u8def\u5f84\r\n     * @type -- \u82e5\u76ee\u6807\u76ee\u5f55\u4e0d\u5b58\u5728\u5219\u65b0\u5efa\r\n     *\/\r\n    function move_file($path,$newpath,$type=true)\r\n    {\r\n        if($type) $this-&gt;dir_mkdirs($newpath);\r\n        $this-&gt;off = @ftp_rename($this-&gt;conn_id,$path,$newpath);\r\n        if(!$this-&gt;off) echo \"\u6587\u4ef6\u79fb\u52a8\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6743\u9650\u53ca\u539f\u8def\u5f84\u662f\u5426\u6b63\u786e\uff01\";\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u590d\u5236\u6587\u4ef6\r\n     * \u8bf4\u660e\uff1a\u7531\u4e8eFTP\u65e0\u590d\u5236\u547d\u4ee4,\u672c\u65b9\u6cd5\u53d8\u901a\u64cd\u4f5c\u4e3a\uff1a\u4e0b\u8f7d\u540e\u518d\u4e0a\u4f20\u5230\u65b0\u7684\u8def\u5f84\r\n     * @path -- \u539f\u8def\u5f84\r\n     * @newpath -- \u65b0\u8def\u5f84\r\n     * @type -- \u82e5\u76ee\u6807\u76ee\u5f55\u4e0d\u5b58\u5728\u5219\u65b0\u5efa\r\n     *\/\r\n    function copy_file($path,$newpath,$type=true)\r\n    {\r\n        $downpath = \"c:\/tmp.dat\";\r\n        $this-&gt;off = @ftp_get($this-&gt;conn_id,$downpath,$path,FTP_BINARY);\/\/ \u4e0b\u8f7d\r\n        if(!$this-&gt;off) echo \"\u6587\u4ef6\u590d\u5236\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6743\u9650\u53ca\u539f\u8def\u5f84\u662f\u5426\u6b63\u786e\uff01\";\r\n        $this-&gt;up_file($downpath,$newpath,$type);\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u5220\u9664\u6587\u4ef6\r\n     * @path -- \u8def\u5f84\r\n     *\/\r\n    function del_file($path)\r\n    {\r\n        $this-&gt;off = @ftp_delete($this-&gt;conn_id,$path);\r\n        if(!$this-&gt;off) echo \"\u6587\u4ef6\u5220\u9664\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6743\u9650\u53ca\u8def\u5f84\u662f\u5426\u6b63\u786e\uff01\";\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u751f\u6210\u76ee\u5f55\r\n     * @path -- \u8def\u5f84\r\n     *\/\r\n    function dir_mkdirs($path)\r\n    {\r\n        $path_arr = explode('\/',$path); \/\/ \u53d6\u76ee\u5f55\u6570\u7ec4\r\n        $file_name = array_pop($path_arr); \/\/ \u5f39\u51fa\u6587\u4ef6\u540d\r\n        $path_div = count($path_arr); \/\/ \u53d6\u5c42\u6570\r\n        foreach($path_arr as $val) \/\/ \u521b\u5efa\u76ee\u5f55\r\n        {\r\n            if(@ftp_chdir($this-&gt;conn_id,$val) == FALSE)\r\n            {\r\n                $tmp = @ftp_mkdir($this-&gt;conn_id,$val);\r\n                if($tmp == FALSE)\r\n                {\r\n                    echo \"\u76ee\u5f55\u521b\u5efa\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6743\u9650\u53ca\u8def\u5f84\u662f\u5426\u6b63\u786e\uff01\";\r\n                    exit;\r\n                }\r\n                @ftp_chdir($this-&gt;conn_id,$val);\r\n            }\r\n        }\r\n        for($i=1;$i=$path_div;$i++) \/\/ \u56de\u9000\u5230\u6839\r\n        {\r\n            @ftp_cdup($this-&gt;conn_id);\r\n        }\r\n    }\r\n    \/**\r\n     * \u65b9\u6cd5\uff1a\u5173\u95edFTP\u8fde\u63a5\r\n     *\/\r\n    function close()\r\n    {\r\n        @ftp_close($this-&gt;conn_id);\r\n    }\r\n}\/\/ class class_ftp end\r\n\/************************************** \u6d4b\u8bd5 ***********************************\r\n$ftp = new class_ftp('192.168.100.143',21,'user','pwd'); \/\/ \u6253\u5f00FTP\u8fde\u63a5\r\n\/\/$ftp-&gt;up_file('aa.txt','a\/b\/c\/cc.txt'); \/\/ \u4e0a\u4f20\u6587\u4ef6\r\n\/\/$ftp-&gt;move_file('a\/b\/c\/cc.txt','a\/cc.txt'); \/\/ \u79fb\u52a8\u6587\u4ef6\r\n\/\/$ftp-&gt;copy_file('a\/cc.txt','a\/b\/dd.txt'); \/\/ \u590d\u5236\u6587\u4ef6\r\n\/\/$ftp-&gt;del_file('a\/b\/dd.txt'); \/\/ \u5220\u9664\u6587\u4ef6\r\n$ftp-&gt;close(); \/\/ \u5173\u95edFTP\u8fde\u63a5\r\n******************************************************************************\/\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * \u4f5c\u7528\uff1aFTP\u64cd\u4f5c\u7c7b( \u62f7\u8d1d\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\/\u521b\u5efa &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=18\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[107],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/18"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":1,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}