{"id":47250,"date":"2024-12-02T10:05:45","date_gmt":"2024-12-02T02:05:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47250\/"},"modified":"2024-12-02T10:05:45","modified_gmt":"2024-12-02T02:05:45","slug":"php-%e5%87%bd%e6%95%b0%e6%89%a9%e5%b1%95%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47250\/","title":{"rendered":"PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/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\u300aPHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f\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>\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u53ef\u63d0\u5347 PHP \u51fd\u6570\u6269\u5c55\u7684\u53ef\u9760\u6027\u4e0e\u6548\u7387\uff1a\u7f16\u5199\u6307\u5357\uff1a\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\uff0c\u91c7\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u907f\u514d\u526f\u4f5c\u7528\u3002\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528\u7f13\u5b58\uff0c\u907f\u514d\u5206\u914d\uff0c\u8003\u8651\u914d\u7f6e\u6587\u4ef6\u3002\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1a\u9a8c\u8bc1\u8f93\u5165\uff0c\u9650\u5236\u6743\u9650\uff0c\u5904\u7406\u5f02\u5e38\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u793a\u4f8b\u6269\u5c55\u6f14\u793a\u4e86\u8fd9\u4e9b\u539f\u5219\u5728\u6587\u4ef6\u4e0a\u4f20\u5904\u7406\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729906999671c49371b191.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u51fd\u6570\u6269\u5c55\u53ef\u4ee5\u5f3a\u5927\u4e14\u6709\u7528\uff0c\u4f46\u5982\u679c\u7f16\u5199\u4e0d\u5f53\uff0c\u5b83\u4eec\u4e5f\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u548c\u5b89\u5168\u6f0f\u6d1e\u3002\u56e0\u6b64\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u5bf9\u4e8e\u6269\u5c55\u7684\u53ef\u9760\u6027\u548c\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u7f16\u5199\u6307\u5357<\/strong><\/p>\n<ul>\n<li><strong>\u4fdd\u6301\u7b80\u5355\uff1a<\/strong>\u8ba9\u6269\u5c55\u6e05\u6670\u6613\u8bfb\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a<\/strong>\u7ec4\u7ec7\u6269\u5c55\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u4e0e\u5176\u4ed6\u51fd\u6570\u548c\u7c7b\u4ea7\u751f\u51b2\u7a81\u3002<\/li>\n<li><strong>\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff1a<\/strong>\u4f7f\u7528 <code>|\\PhpOption\\None<\/code> \u6ce8\u91ca\u53c2\u6570\u7684\u53ef\u9009\u7c7b\u578b\uff0c\u5e76\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u907f\u514d\u526f\u4f5c\u7528\uff1a<\/strong>\u6269\u5c55\u4e0d\u5e94\u8be5\u66f4\u6539\u5916\u90e8\u72b6\u6001\uff0c\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u6216\u6253\u5f00\u7684\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7f13\u5b58\uff1a<\/strong>\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u6216\u9891\u7e41\u4f7f\u7528\u7684\u6269\u5c55\uff0c\u8bf7\u8003\u8651\u7f13\u5b58\u7ed3\u679c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u907f\u514d\u5206\u914d\uff1a<\/strong>\u5c3d\u53ef\u80fd\u907f\u514d\u5206\u914d\u5185\u5b58\uff0c\u8f6c\u800c\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u6216\u4e0d\u53ef\u53d8\u503c\u3002<\/li>\n<li><strong>\u914d\u7f6e\u6587\u4ef6\uff1a<\/strong>\u8003\u8651\u63d0\u4f9b\u914d\u7f6e\u9009\u9879\u4ee5\u6839\u636e\u7cfb\u7edf\u8981\u6c42\u8c03\u6574\u6269\u5c55\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><strong>\u5b89\u5168\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li><strong>\u9a8c\u8bc1\u8f93\u5165\uff1a<\/strong>\u5bf9\u6240\u6709\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li><strong>\u9650\u5236\u6743\u9650\uff1a<\/strong>\u5982\u679c\u6269\u5c55\u9700\u8981\u63d0\u5347\u6743\u9650\uff0c\u8bf7\u4f7f\u7528 <code>access-control<\/code> \u6ce8\u91ca\u6765\u9694\u79bb\u5176\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38\uff1a<\/strong>\u6269\u5c55\u5e94\u8be5\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u5e76\u8bb0\u5f55\u91cd\u8981\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u793a\u4f8b\u6269\u5c55\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff1a<\/p>\n<pre>namespace My\\FileUploadExtension;\n\nuse PhpOption\\None;\n\nfunction upload(string $filename, ?string $destination = null): None|string\n{\n    if (!file_exists($filename)) {\n        return None::create();\n    }\n\n    $extension = pathinfo($filename, PATHINFO_EXTENSION);\n    if ($extension !== 'png' &amp;&amp; $extension !== 'jpg' &amp;&amp; $extension !== 'gif') {\n        throw new \\InvalidArgumentException('Invalid file type');\n    }\n\n    if ($destination === null) {\n        $destination = sys_get_temp_dir() . '\/' . basename($filename);\n    }\n\n    if (!move_uploaded_file($filename, $destination)) {\n        throw new \\RuntimeException('Failed to upload file');\n    }\n\n    return $destination;\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7f16\u5199\u53ef\u9760\u3001\u9ad8\u6548\u4e14\u5b89\u5168\u7684 PHP \u51fd\u6570\u6269\u5c55\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u51c6\u5219\uff0c\u60a8\u53ef\u4ee5\u4e3a\u7528\u6237\u63d0\u4f9b\u5177\u6709\u826f\u597d\u6027\u80fd\u548c\u5f3a\u5927\u529f\u80fd\u7684\u6269\u5c55\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f \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\u300aPHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f\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~ \u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u53ef\u63d0\u5347 PHP \u51fd\u6570\u6269\u5c55\u7684\u53ef\u9760\u6027\u4e0e\u6548\u7387\uff1a\u7f16\u5199\u6307\u5357\uff1a\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\uff0c\u91c7\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u907f\u514d\u526f\u4f5c\u7528\u3002\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528\u7f13\u5b58\uff0c\u907f\u514d\u5206\u914d\uff0c\u8003\u8651\u914d\u7f6e\u6587\u4ef6\u3002\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1a\u9a8c\u8bc1\u8f93\u5165\uff0c\u9650\u5236\u6743\u9650\uff0c\u5904\u7406\u5f02\u5e38\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u793a\u4f8b\u6269\u5c55\u6f14\u793a\u4e86\u8fd9\u4e9b\u539f\u5219\u5728\u6587\u4ef6\u4e0a\u4f20\u5904\u7406\u4e2d\u7684\u5e94\u7528\u3002 PHP \u51fd\u6570\u6269\u5c55\u7684\u6700\u4f73\u5b9e\u8df5 \u5f15\u8a00 PHP \u51fd\u6570\u6269\u5c55\u53ef\u4ee5\u5f3a\u5927\u4e14\u6709\u7528\uff0c\u4f46\u5982\u679c\u7f16\u5199\u4e0d\u5f53\uff0c\u5b83\u4eec\u4e5f\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u548c\u5b89\u5168\u6f0f\u6d1e\u3002\u56e0\u6b64\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u5bf9\u4e8e\u6269\u5c55\u7684\u53ef\u9760\u6027\u548c\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002 \u7f16\u5199\u6307\u5357 \u4fdd\u6301\u7b80\u5355\uff1a\u8ba9\u6269\u5c55\u6e05\u6670\u6613\u8bfb\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a\u7ec4\u7ec7\u6269\u5c55\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u4e0e\u5176\u4ed6\u51fd\u6570\u548c\u7c7b\u4ea7\u751f\u51b2\u7a81\u3002 \u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff1a\u4f7f\u7528 |\\PhpOption\\None \u6ce8\u91ca\u53c2\u6570\u7684\u53ef\u9009\u7c7b\u578b\uff0c\u5e76\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002 \u907f\u514d\u526f\u4f5c\u7528\uff1a\u6269\u5c55\u4e0d\u5e94\u8be5\u66f4\u6539\u5916\u90e8\u72b6\u6001\uff0c\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u6216\u6253\u5f00\u7684\u6587\u4ef6\u3002 \u6027\u80fd\u4f18\u5316 \u4f7f\u7528\u7f13\u5b58\uff1a\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u6216\u9891\u7e41\u4f7f\u7528\u7684\u6269\u5c55\uff0c\u8bf7\u8003\u8651\u7f13\u5b58\u7ed3\u679c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u907f\u514d\u5206\u914d\uff1a\u5c3d\u53ef\u80fd\u907f\u514d\u5206\u914d\u5185\u5b58\uff0c\u8f6c\u800c\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u6216\u4e0d\u53ef\u53d8\u503c\u3002 \u914d\u7f6e\u6587\u4ef6\uff1a\u8003\u8651\u63d0\u4f9b\u914d\u7f6e\u9009\u9879\u4ee5\u6839\u636e\u7cfb\u7edf\u8981\u6c42\u8c03\u6574\u6269\u5c55\u7684\u884c\u4e3a\u3002 \u5b89\u5168\u6ce8\u610f\u4e8b\u9879 \u9a8c\u8bc1\u8f93\u5165\uff1a\u5bf9\u6240\u6709\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002 \u9650\u5236\u6743\u9650\uff1a\u5982\u679c\u6269\u5c55\u9700\u8981\u63d0\u5347\u6743\u9650\uff0c\u8bf7\u4f7f\u7528 access-control \u6ce8\u91ca\u6765\u9694\u79bb\u5176\u64cd\u4f5c\u3002 \u5904\u7406\u5f02\u5e38\uff1a\u6269\u5c55\u5e94\u8be5\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u5e76\u8bb0\u5f55\u91cd\u8981\u5f02\u5e38\u3002 \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4e00\u4e2a\u793a\u4f8b\u6269\u5c55\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff1a namespace My\\FileUploadExtension; use PhpOption\\None; function upload(string $filename, ?string $destination = null): None|string { if (!file_exists($filename)) { return None::create(); } $extension = pathinfo($filename, PATHINFO_EXTENSION); if [&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-47250","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47250","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=47250"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47250\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}