{"id":47164,"date":"2024-12-02T10:33:53","date_gmt":"2024-12-02T02:33:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47164\/"},"modified":"2024-12-02T10:33:53","modified_gmt":"2024-12-02T02:33:53","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-php-%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%e5%b9%b6%e8%8e%b7%e5%8f%96%e5%85%b6%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47164\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>PHP exec() \u51fd\u6570\u53ef\u7528\u4e8e\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u5176\u8bed\u6cd5\u4e3a\uff1aexec(string $command, array &amp;$output = null, int &amp;$return_var = null) : bool\u3002\u53c2\u6570\u5305\u62ec\uff1a$command\uff08\u8981\u6267\u884c\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\uff09\u3001$output\uff08\u63a5\u6536\u547d\u4ee4\u8f93\u51fa\u7684\u5f15\u7528\u53d8\u91cf\u6570\u7ec4\uff09\u548c $return_var\uff08\u63a5\u6536\u547d\u4ee4\u8fd4\u56de\u4ee3\u7801\u7684\u5f15\u7528\u53d8\u91cf\uff09\u3002exec() \u51fd\u6570\u88ab\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u6216\u89e3\u6790\u5916\u90e8\u811a\u672c\u6216\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729902220671c368ce06cf.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570 <code>exec()<\/code> \u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa<\/strong><\/p>\n<p>PHP \u51fd\u6570 <code>exec()<\/code> \u5141\u8bb8\u60a8\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u5728 PHP \u811a\u672c\u4e2d\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u5b83\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u6267\u884c\u7cfb\u7edf\u547d\u4ee4<\/li>\n<li>\u89e3\u6790\u5916\u90e8\u811a\u672c\u6216\u7a0b\u5e8f\u7684\u8f93\u51fa<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>exec(string $command, array &amp;$output = null, int &amp;$return_var = null) : bool<\/pre>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li><code>$command<\/code>: \u8981\u6267\u884c\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>$output<\/code>: \u4e00\u4e2a\u5f15\u7528\u53d8\u91cf\u6570\u7ec4\uff0c\u7528\u4e8e\u63a5\u6536\u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/li>\n<li><code>$return_var<\/code>: \u4e00\u4e2a\u5f15\u7528\u53d8\u91cf\uff0c\u7528\u4e8e\u63a5\u6536\u547d\u4ee4\u7684\u8fd4\u56de\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a PHP \u811a\u672c\u6765\u4f7f\u7528 <code>exec()<\/code> \u51fd\u6570\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5217\u8868\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u6267\u884c 'ls' \u547d\u4ee4\n$output = array();\n$return_var = 0;\nexec('ls', $output, $return_var);\n\n\/\/ \u68c0\u67e5\u547d\u4ee4\u662f\u5426\u6210\u529f\u6267\u884c\nif ($return_var === 0) {\n    \/\/ \u5faa\u73af\u904d\u5386\u8f93\u51fa\u6570\u7ec4\u5e76\u6253\u5370\u6bcf\u4e2a\u6587\u4ef6\n    foreach ($output as $file) {\n        echo $file . \"\\n\";\n    }\n} else {\n    \/\/ \u5904\u7406\u9519\u8bef\n    echo \"\u547d\u4ee4\u57f7\u884c\u5931\u6557\u3002\u932f\u8aa4\u78bc\uff1a$return_var\";\n}\n?&gt;<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>main.php\ncomposer.json\nvendor\/<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5916\u90e8\u547d\u4ee4\u53ef\u7528\u4e14\u5177\u6709\u9002\u5f53\u7684\u6267\u884c\u6743\u9650\u3002<\/li>\n<li><code>exec()<\/code> \u51fd\u6570\u4e0d\u80fd\u7528\u4e8e\u6267\u884c PHP \u4ee3\u7801\u3002\u8981\u6267\u884c PHP \u4ee3\u7801\uff0c\u8bf7\u4f7f\u7528 <code>shell_exec<\/code> \u6216 <code>proc_open<\/code> \u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ PHP exec() \u51fd\u6570\u53ef\u7528\u4e8e\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u5176\u8bed\u6cd5\u4e3a\uff1aexec(string $command, array &amp;$output = null, int &amp;$return_var = null) : bool\u3002\u53c2\u6570\u5305\u62ec\uff1a$command\uff08\u8981\u6267\u884c\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\uff09\u3001$output\uff08\u63a5\u6536\u547d\u4ee4\u8f93\u51fa\u7684\u5f15\u7528\u53d8\u91cf\u6570\u7ec4\uff09\u548c $return_var\uff08\u63a5\u6536\u547d\u4ee4\u8fd4\u56de\u4ee3\u7801\u7684\u5f15\u7528\u53d8\u91cf\uff09\u3002exec() \u51fd\u6570\u88ab\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u6216\u89e3\u6790\u5916\u90e8\u811a\u672c\u6216\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570 exec() \u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8f93\u51fa PHP \u51fd\u6570 exec() \u5141\u8bb8\u60a8\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u5728 PHP \u811a\u672c\u4e2d\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u5b83\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a \u6267\u884c\u7cfb\u7edf\u547d\u4ee4 \u89e3\u6790\u5916\u90e8\u811a\u672c\u6216\u7a0b\u5e8f\u7684\u8f93\u51fa \u8bed\u6cd5\uff1a exec(string $command, array &amp;$output = null, int &amp;$return_var = null) : bool \u53c2\u6570\uff1a $command: \u8981\u6267\u884c\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\u3002 $output: \u4e00\u4e2a\u5f15\u7528\u53d8\u91cf\u6570\u7ec4\uff0c\u7528\u4e8e\u63a5\u6536\u547d\u4ee4\u7684\u8f93\u51fa\u3002 $return_var: \u4e00\u4e2a\u5f15\u7528\u53d8\u91cf\uff0c\u7528\u4e8e\u63a5\u6536\u547d\u4ee4\u7684\u8fd4\u56de\u4ee3\u7801\u3002 [&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-47164","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47164","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=47164"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47164\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}