{"id":3043,"date":"2025-03-19T22:30:06","date_gmt":"2025-03-19T14:30:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3043"},"modified":"2025-03-19T22:30:06","modified_gmt":"2025-03-19T14:30:06","slug":"php-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0\/","title":{"rendered":"PHP \u6587\u4ef6\u4e0a\u4f20"},"content":{"rendered":"\n<p>PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u673a\u5236\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u5e38\u7528\u4e8e\u7528\u6237\u4e0a\u4f20\u56fe\u7247\u3001\u6587\u6863\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u901a\u8fc7 <code>$_FILES<\/code> \u8d85\u5168\u5c40\u53d8\u91cf\uff0cPHP \u53ef\u4ee5\u8f7b\u677e\u63a5\u6536\u5e76\u4fdd\u5b58\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u548c\u6587\u4ef6\u4e0a\u4f20\u7684\u6d41\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 HTML \u6587\u4ef6\u4e0a\u4f20\u8868\u5355<\/strong><\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 HTML \u4e2d\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u5141\u8bb8\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5e76\u63d0\u4ea4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"upload.php\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    Select image to upload:\n    &lt;input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\"&gt;\n    &lt;input type=\"submit\" value=\"Upload Image\" name=\"submit\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>enctype=\"multipart\/form-data\"<\/code><\/strong>\uff1a\u6b64\u5c5e\u6027\u662f\u5fc5\u8981\u7684\uff0c\u5b83\u5141\u8bb8\u8868\u5355\u6570\u636e\u5305\u542b\u6587\u4ef6\u3002\u6ca1\u6709\u5b83\uff0c\u8868\u5355\u65e0\u6cd5\u63d0\u4ea4\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 PHP \u6587\u4ef6\u4e0a\u4f20\u5904\u7406<\/strong><\/h2>\n\n\n\n<p>\u5728\u8868\u5355\u63d0\u4ea4\u540e\uff0cPHP \u5c06\u901a\u8fc7 <code>$_FILES<\/code> \u8d85\u5168\u5c40\u6570\u7ec4\u63a5\u6536\u4e0a\u4f20\u7684\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u4e0a\u4f20\u5904\u7406\u811a\u672c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>upload.php<\/code> \u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    \/\/ \u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\n    $target_dir = \"uploads\/\";  \/\/ \u4e0a\u4f20\u6587\u4ef6\u4fdd\u5b58\u7684\u76ee\u5f55\n    $target_file = $target_dir . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]);  \/\/ \u4e0a\u4f20\u6587\u4ef6\u7684\u76ee\u6807\u8def\u5f84\n    $uploadOk = 1;  \/\/ \u4e0a\u4f20\u72b6\u6001\u6807\u5fd7\uff0c0 \u8868\u793a\u5931\u8d25\uff0c1 \u8868\u793a\u6210\u529f\n    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));  \/\/ \u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u4e3a\u56fe\u7247\n    if (isset($_POST&#91;\"submit\"])) {\n        $check = getimagesize($_FILES&#91;\"fileToUpload\"]&#91;\"tmp_name\"]);\n        if ($check !== false) {\n            echo \"\u6587\u4ef6\u662f\u4e00\u4e2a\u56fe\u7247 - \" . $check&#91;\"mime\"] . \".&lt;br&gt;\";\n            $uploadOk = 1;\n        } else {\n            echo \"\u6587\u4ef6\u4e0d\u662f\u56fe\u7247\u3002&lt;br&gt;\";\n            $uploadOk = 0;\n        }\n    }\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728\n    if (file_exists($target_file)) {\n        echo \"\u62b1\u6b49\uff0c\u6587\u4ef6\u5df2\u5b58\u5728\u3002&lt;br&gt;\";\n        $uploadOk = 0;\n    }\n\n    \/\/ \u9650\u5236\u6587\u4ef6\u5927\u5c0f\uff08\u4f8b\u5982\u6700\u5927\u4e3a 5MB\uff09\n    if ($_FILES&#91;\"fileToUpload\"]&#91;\"size\"] &gt; 5000000) {  \/\/ 5MB\n        echo \"\u62b1\u6b49\uff0c\u60a8\u7684\u6587\u4ef6\u592a\u5927\u3002&lt;br&gt;\";\n        $uploadOk = 0;\n    }\n\n    \/\/ \u53ea\u5141\u8bb8\u7279\u5b9a\u6587\u4ef6\u7c7b\u578b\uff08\u4f8b\u5982\uff1ajpg\u3001png\u3001jpeg\u3001gif\uff09\n    if ($imageFileType != \"jpg\" &amp;&amp; $imageFileType != \"png\" &amp;&amp; $imageFileType != \"jpeg\" &amp;&amp; $imageFileType != \"gif\") {\n        echo \"\u62b1\u6b49\uff0c\u53ea\u6709 JPG\u3001JPEG\u3001PNG \u548c GIF \u6587\u4ef6\u53ef\u4ee5\u4e0a\u4f20\u3002&lt;br&gt;\";\n        $uploadOk = 0;\n    }\n\n    \/\/ \u68c0\u67e5 `$uploadOk` \u662f\u5426\u4e3a 0\uff08\u5373\u4e0a\u4f20\u5931\u8d25\uff09\n    if ($uploadOk == 0) {\n        echo \"\u62b1\u6b49\uff0c\u60a8\u7684\u6587\u4ef6\u672a\u80fd\u4e0a\u4f20\u3002&lt;br&gt;\";\n    } else {\n        \/\/ \u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u5c1d\u8bd5\u4e0a\u4f20\u6587\u4ef6\n        if (move_uploaded_file($_FILES&#91;\"fileToUpload\"]&#91;\"tmp_name\"], $target_file)) {\n            echo \"\u6587\u4ef6 \" . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]) . \" \u5df2\u4e0a\u4f20\u3002&lt;br&gt;\";\n        } else {\n            echo \"\u62b1\u6b49\uff0c\u4e0a\u4f20\u6587\u4ef6\u65f6\u51fa\u9519\u3002&lt;br&gt;\";\n        }\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 \u5173\u952e\u6b65\u9aa4\u89e3\u6790<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u83b7\u53d6\u6587\u4ef6\u4fe1\u606f<\/strong><\/h3>\n\n\n\n<p>PHP \u901a\u8fc7 <code>$_FILES<\/code> \u6570\u7ec4\u6765\u8bbf\u95ee\u4e0a\u4f20\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8fd9\u4e2a\u6570\u7ec4\u5305\u542b\u591a\u4e2a\u5b50\u6570\u7ec4\uff0c\u5176\u4e2d\u4e3b\u8981\u7684\u5b57\u6bb5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_FILES[\"fileToUpload\"][\"name\"]<\/code>\uff1a\u4e0a\u4f20\u6587\u4ef6\u7684\u539f\u59cb\u540d\u79f0\u3002<\/li>\n\n\n\n<li><code>$_FILES[\"fileToUpload\"][\"type\"]<\/code>\uff1a\u4e0a\u4f20\u6587\u4ef6\u7684 MIME \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>$_FILES[\"fileToUpload\"][\"tmp_name\"]<\/code>\uff1a\u6587\u4ef6\u7684\u4e34\u65f6\u5b58\u50a8\u8def\u5f84\u3002<\/li>\n\n\n\n<li><code>$_FILES[\"fileToUpload\"][\"size\"]<\/code>\uff1a\u6587\u4ef6\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u6587\u4ef6\u68c0\u67e5<\/strong><\/h3>\n\n\n\n<p>\u5728\u5b9e\u9645\u4e0a\u4f20\u6587\u4ef6\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u68c0\u67e5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u7c7b\u578b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 <code>getimagesize()<\/code> \u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u662f\u6709\u6548\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5927\u5c0f<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9650\u5236\u4e0a\u4f20\u7684\u6587\u4ef6\u5927\u5c0f\uff08\u4f8b\u5982\uff1a\u4e0d\u8d85\u8fc7 5MB\uff09\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728<\/strong>\uff1a\u907f\u514d\u8986\u76d6\u540c\u540d\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u6269\u5c55\u540d\u68c0\u67e5<\/strong>\uff1a\u9650\u5236\u53ea\u5141\u8bb8\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u4e0a\u4f20\uff08\u5982\u56fe\u7247\u6587\u4ef6\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u4fdd\u5b58\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>move_uploaded_file()<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u4ece\u4e34\u65f6\u76ee\u5f55\u79fb\u52a8\u5230\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>move_uploaded_file($_FILES&#91;\"fileToUpload\"]&#91;\"tmp_name\"], $target_file);<\/code><\/pre>\n\n\n\n<p>\u6b64\u51fd\u6570\u4f1a\u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u6587\u4ef6\u4e0a\u4f20\u7684\u5b89\u5168\u6027\u8003\u8651<\/strong><\/h2>\n\n\n\n<p>\u867d\u7136 PHP \u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\uff0c\u4f46\u7531\u4e8e\u5b89\u5168\u539f\u56e0\uff0c\u60a8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u9650\u5236\u6587\u4ef6\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u786e\u4fdd\u4e0a\u4f20\u7684\u6587\u4ef6\u662f\u60a8\u671f\u671b\u7684\u6587\u4ef6\u7c7b\u578b\uff08\u4f8b\u5982\uff1a\u56fe\u7247\u3001PDF \u6587\u4ef6\u7b49\uff09\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5 MIME \u7c7b\u578b\u6216\u6587\u4ef6\u6269\u5c55\u540d\u6765\u9650\u5236\u4e0a\u4f20\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u76f4\u63a5\u6267\u884c\u4e0a\u4f20\u7684\u6587\u4ef6<\/strong>\uff0c\u4f8b\u5982\u4e0d\u5141\u8bb8\u4e0a\u4f20\u53ef\u6267\u884c\u7684 PHP \u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u9650\u5236\u6587\u4ef6\u5927\u5c0f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bbe\u7f6e\u5408\u9002\u7684\u6587\u4ef6\u5927\u5c0f\u9650\u5236\uff0c\u4ee5\u9632\u6b62\u7528\u6237\u4e0a\u4f20\u8fc7\u5927\u7684\u6587\u4ef6\uff0c\u6d88\u8017\u8fc7\u591a\u7684\u670d\u52a1\u5668\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4f7f\u7528 <code>$_FILES[\"fileToUpload\"][\"size\"]<\/code> \u6765\u9650\u5236\u6587\u4ef6\u5927\u5c0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u9690\u85cf\u4e0a\u4f20\u76ee\u5f55<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u8981\u5141\u8bb8\u4e0a\u4f20\u7684\u6587\u4ef6\u5728\u7f51\u9875\u4e2d\u76f4\u63a5\u8bbf\u95ee\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u670d\u52a1\u5668\u4e0a\u5bf9\u6587\u4ef6\u76ee\u5f55\u8bbe\u7f6e\u6743\u9650\uff08\u5982 <code>.htaccess<\/code>\uff09\u6765\u907f\u514d\u516c\u5f00\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.4 \u6587\u4ef6\u540d\u91cd\u547d\u540d<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0a\u4f20\u7684\u6587\u4ef6\u53ef\u80fd\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff0c\u8003\u8651\u7ed9\u6587\u4ef6\u91cd\u547d\u540d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>uniqid()<\/code> \u51fd\u6570\u751f\u6210\u552f\u4e00\u7684\u6587\u4ef6\u540d\uff0c\u6216\u8005\u4f7f\u7528\u7528\u6237 ID \u7b49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$target_file = $target_dir . uniqid() . \".\" . $imageFileType;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 \u5c0f\u7ed3<\/strong><\/h2>\n\n\n\n<p>PHP \u6587\u4ef6\u4e0a\u4f20\u673a\u5236\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5f00\u53d1\u8005\u5fc5\u987b\u786e\u4fdd\u5904\u7406\u597d\u6587\u4ef6\u7684\u5b89\u5168\u6027\u3001\u6709\u6548\u6027\u548c\u4e0a\u4f20\u9650\u5236\u3002\u901a\u8fc7 <code>$_FILES<\/code> \u8d85\u5168\u5c40\u53d8\u91cf\uff0cPHP \u53ef\u4ee5\u5904\u7406\u591a\u79cd\u6587\u4ef6\u7c7b\u578b\u7684\u4e0a\u4f20\uff0c\u5305\u62ec\u56fe\u50cf\u3001\u6587\u6863\u7b49\u3002\u5bf9\u4e8e\u5b9e\u9645\u5f00\u53d1\uff0c\u5f3a\u70c8\u5efa\u8bae\u5bf9\u6587\u4ef6\u7c7b\u578b\u3001\u5927\u5c0f\u7b49\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u76f8\u5173\u94fe\u63a5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/features.file-upload.php\">PHP \u6587\u4ef6\u4e0a\u4f20\u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/reserved.variables.files.php\">PHP $_FILES \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u673a\u5236\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u5e38\u7528\u4e8e\u7528\u6237\u4e0a\u4f20\u56fe\u7247\u3001\u6587\u6863\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u901a\u8fc7 $_FILES [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3044,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3043"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3043\/revisions"}],"predecessor-version":[{"id":3045,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3043\/revisions\/3045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3044"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}