{"id":22275,"date":"2024-11-21T08:21:55","date_gmt":"2024-11-21T00:21:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22275\/"},"modified":"2024-11-21T08:21:55","modified_gmt":"2024-11-21T00:21:55","slug":"thinkphp5-%e5%a4%a7%e6%8b%ac%e5%8f%b7%e9%94%99%e8%af%af%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22275\/","title":{"rendered":"thinkphp5 \u5927\u62ec\u53f7\u9519\u8bef\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<p>\u5728\u4f7f\u7528thinkphp5\u8fdb\u884c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5927\u5bb6\u90fd\u6709\u53ef\u80fd\u9047\u5230\u8fc7\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u5c31\u662f\u8bed\u6cd5\u9519\u8bef\u3002\u5176\u4e2d\uff0c\u5bf9\u4e8e\u4ee3\u7801\u5757\u5927\u62ec\u53f7\u7684\u4f7f\u7528\uff0c\u6709\u65f6\u5019\u4e5f\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5c0f\u95ee\u9898\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecdthinkphp5\u5927\u62ec\u53f7\u9519\u8bef\u7684\u60c5\u51b5\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u95ee\u9898\u63cf\u8ff0<\/li>\n<\/ol>\n<p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u5019\u4f1a\u9047\u5230\u5728\u4ee3\u7801\u5757\u5916\u6216\u5185\u7f3a\u5c11\u5927\u62ec\u53f7\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>    if($file-&gt;validate(['size'=&gt;2048000,'ext'=&gt;'jpg,png,gif'])-&gt;move($path)){\n      return json_encode(['code' =&gt; 1, 'msg' =&gt; '\u4e0a\u4f20\u6210\u529f']);\n    }\n    else\n      return json_encode(['code' =&gt; 0, 'msg' =&gt; '\u4e0a\u4f20\u5931\u8d25']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u7531\u4e8eelse\u8bed\u53e5\u7f3a\u5c11\u5927\u62ec\u53f7\uff0c\u7cfb\u7edf\u89e3\u6790\u65f6\u4e0d\u80fd\u6b63\u786e\u8bc6\u522b\u8fd9\u4e2a\u8bed\u53e5\uff0c\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<ol>\n<li>\u89e3\u51b3\u65b9\u6cd5<\/li>\n<\/ol>\n<p>\u5728\u89e3\u51b3\u5927\u62ec\u53f7\u9519\u8bef\u7684\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u786e\u4fdd\u4ee3\u7801\u5757\u5185\u5916\u7684\u5927\u62ec\u53f7\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u5373\u53ef\u3002\u66f4\u5177\u4f53\u7684\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>1) \u4fdd\u8bc1\u4ee3\u7801\u5757\u5916\u7684\u5927\u62ec\u53f7\u5b58\u5728<\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>    if($file-&gt;validate(['size'=&gt;2048000,'ext'=&gt;'jpg,png,gif'])-&gt;move($path)){\n      return json_encode(['code' =&gt; 1, 'msg' =&gt; '\u4e0a\u4f20\u6210\u529f']);\n    }\n    else\n      return json_encode(['code' =&gt; 0, 'msg' =&gt; '\u4e0a\u4f20\u5931\u8d25']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06else\u8bed\u53e5\u7684\u5927\u62ec\u53f7\u52a0\u4e0a\uff0c\u5373\u53ef\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>    if($file-&gt;validate(['size'=&gt;2048000,'ext'=&gt;'jpg,png,gif'])-&gt;move($path)){\n      return json_encode(['code' =&gt; 1, 'msg' =&gt; '\u4e0a\u4f20\u6210\u529f']);\n    }\n    else {\n      return json_encode(['code' =&gt; 0, 'msg' =&gt; '\u4e0a\u4f20\u5931\u8d25']);\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2) \u4fdd\u8bc1\u4ee3\u7801\u5757\u5185\u7684\u5927\u62ec\u53f7\u5b58\u5728<\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>    $data = [\n      ['id' =&gt; 1, 'name' =&gt; '\u5c0f\u660e'],\n      ['id' =&gt; 2, 'name' =&gt; '\u5c0f\u7ea2'],\n      ['id' =&gt; 3, 'name' =&gt; '\u5c0f\u521a']\n    ]\n    \n    foreach($data as $val)\n      $name = $val['name'];\n      echo $name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728foreach\u8bed\u53e5\u4e2d\u7684\u4ee3\u7801\u5757\u52a0\u4e0a\u5927\u62ec\u53f7\uff0c\u5373\u53ef\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>    $data = [\n      ['id' =&gt; 1, 'name' =&gt; '\u5c0f\u660e'],\n      ['id' =&gt; 2, 'name' =&gt; '\u5c0f\u7ea2'],\n      ['id' =&gt; 3, 'name' =&gt; '\u5c0f\u521a']\n    ]\n    \n    foreach($data as $val) {\n      $name = $val['name'];\n      echo $name;\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528ThinkPHP5\u7f16\u5199\u4ee3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef\u7684\u60c5\u51b5\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u5728\u5904\u7406\u5927\u62ec\u53f7\u9519\u8bef\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6309\u7167\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6cd5\u4fdd\u8bc1\u4ee3\u7801\u5757\u5185\u5916\u7684\u5927\u62ec\u53f7\u6210\u5bf9\u51fa\u73b0\uff0c\u5373\u53ef\u6709\u6548\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp5 \u5927\u62ec\u53f7\u9519\u8bef\u600e\u4e48\u89e3\u51b3\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>\u5728\u4f7f\u7528thinkphp5\u8fdb\u884c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5927\u5bb6\u90fd\u6709\u53ef\u80fd\u9047\u5230\u8fc7\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u5c31\u662f\u8bed\u6cd5\u9519\u8bef\u3002\u5176\u4e2d\uff0c\u5bf9\u4e8e\u4ee3\u7801\u5757\u5927\u62ec\u53f7\u7684\u4f7f\u7528\uff0c\u6709\u65f6\u5019\u4e5f\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5c0f\u95ee\u9898\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecdthinkphp5\u5927\u62ec\u53f7\u9519\u8bef\u7684\u60c5\u51b5\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002 \u95ee\u9898\u63cf\u8ff0 \u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u5019\u4f1a\u9047\u5230\u5728\u4ee3\u7801\u5757\u5916\u6216\u5185\u7f3a\u5c11\u5927\u62ec\u53f7\u5bfc\u81f4\u7684\u9519\u8bef\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a if($file-&gt;validate([&#8216;size&#8217;=&gt;2048000,&#8217;ext&#8217;=&gt;&#8217;jpg,png,gif&#8217;])-&gt;move($path)){ return json_encode([&#8216;code&#8217; =&gt; 1, &#8216;msg&#8217; =&gt; &#8216;\u4e0a\u4f20\u6210\u529f&#8217;]); } else return json_encode([&#8216;code&#8217; =&gt; 0, &#8216;msg&#8217; =&gt; &#8216;\u4e0a\u4f20\u5931\u8d25&#8217;]); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u7531\u4e8eelse\u8bed\u53e5\u7f3a\u5c11\u5927\u62ec\u53f7\uff0c\u7cfb\u7edf\u89e3\u6790\u65f6\u4e0d\u80fd\u6b63\u786e\u8bc6\u522b\u8fd9\u4e2a\u8bed\u53e5\uff0c\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u9519\u8bef\u3002 \u89e3\u51b3\u65b9\u6cd5 \u5728\u89e3\u51b3\u5927\u62ec\u53f7\u9519\u8bef\u7684\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u786e\u4fdd\u4ee3\u7801\u5757\u5185\u5916\u7684\u5927\u62ec\u53f7\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u5373\u53ef\u3002\u66f4\u5177\u4f53\u7684\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1) \u4fdd\u8bc1\u4ee3\u7801\u5757\u5916\u7684\u5927\u62ec\u53f7\u5b58\u5728 \u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a if($file-&gt;validate([&#8216;size&#8217;=&gt;2048000,&#8217;ext&#8217;=&gt;&#8217;jpg,png,gif&#8217;])-&gt;move($path)){ return json_encode([&#8216;code&#8217; =&gt; 1, &#8216;msg&#8217; =&gt; &#8216;\u4e0a\u4f20\u6210\u529f&#8217;]); } else return json_encode([&#8216;code&#8217; =&gt; 0, &#8216;msg&#8217; =&gt; &#8216;\u4e0a\u4f20\u5931\u8d25&#8217;]); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ef\u4ee5\u5c06else\u8bed\u53e5\u7684\u5927\u62ec\u53f7\u52a0\u4e0a\uff0c\u5373\u53ef\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002\u4ee3\u7801\u5982\u4e0b\uff1a if($file-&gt;validate([&#8216;size&#8217;=&gt;2048000,&#8217;ext&#8217;=&gt;&#8217;jpg,png,gif&#8217;])-&gt;move($path)){ return json_encode([&#8216;code&#8217; =&gt; 1, [&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-22275","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22275","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=22275"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22275\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}