{"id":24052,"date":"2024-11-21T16:42:53","date_gmt":"2024-11-21T08:42:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24052\/"},"modified":"2024-11-21T16:42:53","modified_gmt":"2024-11-21T08:42:53","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8swoole%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e7%9a%84swoole_ftpput%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24052\/","title":{"rendered":"\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_ftpput\u51fd\u6570"},"content":{"rendered":"<p>\u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u6765\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u901a\u4fe1\u3002\u5728swoole\u4e2d\uff0c\u6709\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u51fd\u6570swoole_ftpput\u7528\u4e8e\u5b9e\u73b0ftp\u6587\u4ef6\u4e0a\u4f20\uff0c\u4f46\u662f\u5b83\u5355\u72ec\u8c03\u7528\u65f6\u5e76\u4e0d\u652f\u6301\u534f\u7a0b\uff0c\u56e0\u6b64\u65e0\u6cd5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u65f6\u670d\u52a1\u7684\u4f18\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_ftpput\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u670d\u52a1\u6027\u80fd\u3002<\/p>\n<p>\u4e00\u3001swoole_ftpput\u51fd\u6570<\/p>\n<p>swoole_ftpput\u51fd\u6570\u662fSwoole\u63d0\u4f9b\u7684\u4e00\u4e2a\u7528\u4e8eFTP\u6587\u4ef6\u4e0a\u4f20\u7684\u51fd\u6570\uff0c\u901a\u8fc7\u8be5\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5c06\u672c\u5730\u6587\u4ef6\u4e0a\u4f20\u5230FTP\u670d\u52a1\u5668\u7684\u529f\u80fd\u3002swoole_ftpput\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre>bool swoole_ftpput($ftp_stream, $remote_file, $local_file, $mode = -1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$ftp_stream\u8868\u793aFTP\u8fde\u63a5\u7684\u8d44\u6e90\u53e5\u67c4\uff0c$remote_file\u8868\u793a\u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\uff0c$local_file\u8868\u793a\u672c\u5730\u6587\u4ef6\u8def\u5f84\uff0c$mode\u8868\u793a\u4f20\u8f93\u6a21\u5f0f\uff0c-1\u8868\u793a\u4f7f\u7528\u88ab\u52a8\u6a21\u5f0f\uff0c0\u8868\u793a\u4f7f\u7528\u4e3b\u52a8\u6a21\u5f0f\u3002swoole_ftpput\u51fd\u6570\u662f\u963b\u585e\u5f0f\u7684\uff0c\u56e0\u6b64\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u540c\u65f6\u4e0a\u4f20\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u7b49\u5f85\u5f53\u524d\u6587\u4ef6\u4e0a\u4f20\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u4e0b\u4e00\u4e2a\u6587\u4ef6\u7684\u4e0a\u4f20\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u4e0a\u4f20<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u6587\u4ef6\u4e0a\u4f20\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swoole\u534f\u7a0b\u6765\u4f18\u5316swoole_ftpput\u51fd\u6570\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5728FTP\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u7f16\u5199\u4e00\u4e2a\u534f\u7a0b\u4efb\u52a1\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u4e0a\u4f20\u4e00\u4e2a\u6587\u4ef6\u5230FTP\u670d\u52a1\u5668\u3002<\/li>\n<\/ol>\n<pre>function ftpUpload($ftp, $filename) {\n    $local_file = '\/path\/to\/local\/file\/' . $filename;\n    $remote_file = '\/path\/to\/remote\/file\/' . $filename;\n\n    $ret = swoole_coroutine_syscall('file_get_contents', $local_file);\n    if($ret === false){\n        echo \"upload failed: file_get_contents failed\n\";\n        return;\n    }\n\n    $ret = swoole_coroutine_syscall('swoole_ftpput', $ftp, $remote_file, $local_file);\n    if($ret === false){\n        echo \"upload failed: swoole_ftpput failed\n\";\n        return;\n    }\n\n    echo \"upload success: $filename\n\";\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528file_get_contents\u51fd\u6570\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528swoole_ftpput\u51fd\u6570\u5c06\u5185\u5bb9\u4e0a\u4f20\u5230FTP\u670d\u52a1\u5668\u3002\u5728\u4f7f\u7528swoole_ftpput\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u4f7f\u7528swoole_coroutine_syscall\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u534f\u7a0b\u65b9\u5f0f\u6267\u884c\u3002<\/p>\n<ol>\n<li>\u5728\u4e3b\u51fd\u6570\u4e2d\u5e76\u53d1\u6267\u884c\u4e0a\u4f20\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<p>\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2afor\u5faa\u73af\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4e0a\u4f20\u4efb\u52a1\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u4e0a\u4f20\u7684\u76ee\u7684\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>$ftp = ftp_connect($ftp_host, $ftp_port);\nftp_login($ftp, $ftp_user, $ftp_pass);\nftp_pasv($ftp, true);\n\n$scheduler = new SwooleCoroutineScheduler();\n\nfor($i = 1; $i add(\"ftpUpload\", $ftp, \"file$i.txt\");\n}\n\n$scheduler-&amp;gt;start();\n\nftp_close($ftp);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528ftp_connect\u51fd\u6570\u8fde\u63a5FTP\u670d\u52a1\u5668\uff0c\u7136\u540e\u4f7f\u7528ftp_login\u51fd\u6570\u8fdb\u884c\u767b\u5f55\uff0c\u6700\u540e\u4f7f\u7528ftp_pasv\u51fd\u6570\u5f00\u542f\u88ab\u52a8\u6a21\u5f0f\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aSwooleCoroutineScheduler\u5bf9\u8c61\uff0c\u901a\u8fc7add\u65b9\u6cd5\u5f80\u8c03\u5ea6\u5668\u4e2d\u6dfb\u52a0\u4e0a\u4f20\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528start\u65b9\u6cd5\u542f\u52a8\u8c03\u5ea6\u5668\uff0c\u5728\u8c03\u5ea6\u5668\u4e2d\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4f18\u5316FTP\u6587\u4ef6\u4e0a\u4f20\u670d\u52a1\u7684\u6027\u80fd\uff0c\u63d0\u9ad8\u670d\u52a1\u7684\u5e76\u53d1\u80fd\u529b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_ftpput\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u591a\u6587\u4ef6\u4e0a\u4f20\u7684\u670d\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u63d0\u9ad8\u670d\u52a1\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684_ftpput\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u6765\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u901a\u4fe1\u3002\u5728swoole\u4e2d\uff0c\u6709\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u51fd\u6570swoole_ftpput\u7528\u4e8e\u5b9e\u73b0ftp\u6587\u4ef6\u4e0a\u4f20\uff0c\u4f46\u662f\u5b83\u5355\u72ec\u8c03\u7528\u65f6\u5e76\u4e0d\u652f\u6301\u534f\u7a0b\uff0c\u56e0\u6b64\u65e0\u6cd5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u65f6\u670d\u52a1\u7684\u4f18\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_ftpput\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u670d\u52a1\u6027\u80fd\u3002 \u4e00\u3001swoole_ftpput\u51fd\u6570 swoole_ftpput\u51fd\u6570\u662fSwoole\u63d0\u4f9b\u7684\u4e00\u4e2a\u7528\u4e8eFTP\u6587\u4ef6\u4e0a\u4f20\u7684\u51fd\u6570\uff0c\u901a\u8fc7\u8be5\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5c06\u672c\u5730\u6587\u4ef6\u4e0a\u4f20\u5230FTP\u670d\u52a1\u5668\u7684\u529f\u80fd\u3002swoole_ftpput\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a bool swoole_ftpput($ftp_stream, $remote_file, $local_file, $mode = -1); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$ftp_stream\u8868\u793aFTP\u8fde\u63a5\u7684\u8d44\u6e90\u53e5\u67c4\uff0c$remote_file\u8868\u793a\u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\uff0c$local_file\u8868\u793a\u672c\u5730\u6587\u4ef6\u8def\u5f84\uff0c$mode\u8868\u793a\u4f20\u8f93\u6a21\u5f0f\uff0c-1\u8868\u793a\u4f7f\u7528\u88ab\u52a8\u6a21\u5f0f\uff0c0\u8868\u793a\u4f7f\u7528\u4e3b\u52a8\u6a21\u5f0f\u3002swoole_ftpput\u51fd\u6570\u662f\u963b\u585e\u5f0f\u7684\uff0c\u56e0\u6b64\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u540c\u65f6\u4e0a\u4f20\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u7b49\u5f85\u5f53\u524d\u6587\u4ef6\u4e0a\u4f20\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u4e0b\u4e00\u4e2a\u6587\u4ef6\u7684\u4e0a\u4f20\u3002 \u4e8c\u3001\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u4e0a\u4f20 \u4e3a\u4e86\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u6587\u4ef6\u4e0a\u4f20\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swoole\u534f\u7a0b\u6765\u4f18\u5316swoole_ftpput\u51fd\u6570\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a \u5728FTP\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u4e0a\u4f20\u7684\u6587\u4ef6\u3002 \u7f16\u5199\u4e00\u4e2a\u534f\u7a0b\u4efb\u52a1\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u4e0a\u4f20\u4e00\u4e2a\u6587\u4ef6\u5230FTP\u670d\u52a1\u5668\u3002 function ftpUpload($ftp, $filename) { $local_file = &#8216;\/path\/to\/local\/file\/&#8217; . $filename; $remote_file = &#8216;\/path\/to\/remote\/file\/&#8217; . $filename; $ret = swoole_coroutine_syscall(&#8216;file_get_contents&#8217;, $local_file); if($ret === false){ echo &#8220;upload failed: file_get_contents failed &#8220;; return; } $ret = swoole_coroutine_syscall(&#8216;swoole_ftpput&#8217;, $ftp, $remote_file, $local_file); if($ret === false){ echo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24052","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24052"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24052\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}