{"id":481,"date":"2017-02-09T21:06:44","date_gmt":"2017-02-09T13:06:44","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=481"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"php-article44","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/09\/php-article44\/","title":{"rendered":"PHP\u5b9e\u73b0\u672c\u5730\u56fe\u7247\u7684\u4e0a\u4f20\u548c\u9a8c\u8bc1\u529f\u80fd"},"content":{"rendered":"<h2>\u4eca\u5929\u7814\u7a76\u4e86\u4e00\u4e0bPHP\u7684FILES\u51fd\u6570\uff0c\u4f7f\u7528PHP\u5b9e\u73b0\u672c\u5730\u56fe\u7247\u7684\u4e0a\u4f20\uff0c\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u4e3a\u56fe\u7247\uff0c\u5e76\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u6307\u5b9a\u76ee\u5f55\u4e0b\uff0c\u5b9e\u73b0\u6e38\u89c8\u56fe\u7247\u7684\u529f\u80fd\uff0c\u611f\u89c9\u8fd8\u662f\u5f88\u597d\u73a9\u7684\uff0c\u4e00\u5171\u6709\u4e24\u4e2aPHP\u9875\u9762\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u524d\u53f0\u7684\u4e0a\u4f20\u548c\u56fe\u7247\u5c55\u793a\uff0c\u53e6\u4e00\u4e2a\u7528\u6765\u5904\u7406\u4fdd\u5b58\u56fe\u7247\uff0c\u4ee3\u7801\u91cc\u505a\u4e86\u5f88\u8be6\u7ec6\u7684\u6ce8\u91ca\uff0c\u5e76\u5c06FILES\u51fd\u6570\u7684\u8bed\u6cd5\uff0cerror\u62a5\u9519\u7c7b\u578b\u5199\u4e86\u51fa\u6765\uff0c\u8fd8\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u3002<\/h2>\n<h3>FILES\u51fd\u6570<\/h3>\n<ol>\n<li><code>$_FILES['myFile']['name']<\/code> \u5ba2\u6237\u7aef\u6587\u4ef6\u7684\u539f\u540d\u79f0<\/li>\n<li><code>$_FILES['myFile']['type']<\/code> \u6587\u4ef6\u7684 MIME\u7c7b\u578b\uff0c\u9700\u8981\u6d4f\u89c8\u5668\u63d0\u4f9b\u8be5\u4fe1\u606f\u7684\u652f\u6301\uff0c\u4f8b\u5982\"image\/gif\"<\/li>\n<li><code>$_FILES['myFile']['size']<\/code> \u5df2\u4e0a\u4f20\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u4e3a\u5b57\u8282<\/li>\n<li><code>$_FILES['myFile']['tmp_name']<\/code> \u6587\u4ef6\u88ab\u4e0a\u4f20\u540e\u5728\u670d\u52a1\u7aef\u50a8\u5b58\u7684\u4e34\u65f6\u6587\u4ef6\u540d\uff0c\u4e00\u822c\u662f\u7cfb\u7edf\u9ed8\u8ba4\uff0c\u53ef\u4ee5\u5728php.ini\u7684upload_tmp_dir\u6307\u5b9a<\/li>\n<\/ol>\n<h3>\u5173\u4e8eFILES\u4e2derror\u62a5\u9519\u7c7b\u578b\u53ca\u5176\u6ce8\u610f\uff1a<\/h3>\n<h4>\u8fc7\u6ee4\u9519\u8bef\u53f7<\/h4>\n<ul>\n<li>0: \u6ca1\u6709\u9519\u8bef\u53d1\u751f\uff0c\u6587\u4ef6\u4e0a\u4f20\u6210\u529f<\/li>\n<li>1: \u4e0a\u4f20\u7684\u6587\u4ef6\u8d85\u8fc7\u4e86 php.ini \u4e2d upload_max_filesize\u9009\u9879\u9650\u5236\u7684\u503c<\/li>\n<li>2: \u4e0a\u4f20\u6587\u4ef6\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86 HTML \u8868\u5355\u4e2d MAX_FILE_SIZE \u9009\u9879\u6307\u5b9a\u7684\u503c<\/li>\n<li>3: \u6587\u4ef6\u53ea\u6709\u90e8\u5206\u88ab\u4e0a\u4f20<\/li>\n<li>4: \u6ca1\u6709\u6587\u4ef6\u88ab\u4e0a\u4f20<\/li>\n<li>5: \u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u4e3a0<\/li>\n<\/ul>\n<h3>\u6ce8\u610f\uff1a<\/h3>\n<ol>\n<li>\u6587\u4ef6\u88ab\u4e0a\u4f20\u7ed3\u675f\u540e\uff0c\u9ed8\u8ba4\u5730\u88ab\u5b58\u50a8\u5728\u4e86\u4e34\u65f6\u76ee\u5f55\u4e2d\uff0c\u8fd9\u65f6\u5fc5\u987b\u5c06\u5b83\u4ece\u4e34\u65f6\u76ee\u5f55\u4e2d\u5220\u9664\u6216\u79fb\u52a8\u5230\u5176\u5b83\u5730\u65b9\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4f1a\u88ab\u5220\u9664\u3002\u4e5f\u5c31\u662f\u4e0d\u7ba1\u662f\u5426\u4e0a\u4f20\u6210\u529f\uff0c\u811a\u672c\u6267\u884c\u5b8c\u540e\u4e34\u65f6\u76ee\u5f55\u91cc\u7684\u6587\u4ef6\u80af\u5b9a\u4f1a\u88ab\u5220\u9664\u3002\u6240\u4ee5\u5728\u5220\u9664\u4e4b\u524d\u8981\u7528PHP\u7684copy() \u51fd\u6570\u5c06\u5b83\u590d\u5236\u5230\u5176\u5b83\u4f4d\u7f6e\uff0c\u6b64\u65f6\uff0c\u624d\u7b97\u5b8c\u6210\u4e86\u4e0a\u4f20\u6587\u4ef6\u8fc7\u7a0b\u3002<\/li>\n<li>\u5728 PHP 4.1.0 \u7248\u672c\u4ee5\u524d\u8be5\u6570\u7ec4\u7684\u540d\u79f0\u4e3a<code>$HTTP_POST_FILES<\/code>\uff0c\u5b83\u5e76\u4e0d\u50cf$_FILES \u4e00\u6837\u662f\u81ea\u52a8\u5168\u5c40\u53d8\u91cf\u3002PHP 3 \u4e0d\u652f\u6301<code>$HTTP_POST_FILES<\/code>\u6570\u7ec4\u3002<\/li>\n<li>\u7528form\u4e0a\u4f20\u6587\u4ef6\u65f6\uff0c\u4e00\u5b9a\u8981\u52a0\u4e0a\u5c5e\u6027\u5185\u5bb9<code>enctype=\"multipart\/form-data\"<\/code>\uff0c\u5426\u5219\u7528<code>$_FILES[filename]<\/code>\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u65f6\u4f1a\u62a5\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<h3>\u6548\u679c\u5982\u4e0b\u56fe<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/php9.jpg\" width=\"683\" height=\"555\" \/><\/p>\n<h3>\u4ee5\u4e0b\u662f\u4ee3\u7801<\/h3>\n<h2>index.php<\/h2>\n<pre><code class=\"PHP\">&lt;!--?php header(\"Content-Type:text\/html;charset=utf-8\"); ?--&gt;\n&lt;pre class=\"lang:php decode:true \"&gt;&lt;?php\nheader(\"Content-Type:text\/html;charset=utf-8\");\n?&gt;\n&lt;title&gt;PHP\u5b9e\u73b0\u56fe\u7247\u7684\u4e0a\u4f20\u548c\u9a8c\u8bc1&lt;\/title&gt;\n&lt;style&gt;\ntable{text-align:center;}\n&lt;\/style&gt;\n&lt;div align=\"center\"&gt;\n&lt;h1&gt;\u4e0a\u4f20\u529f\u80fd&lt;\/h1&gt;\n&lt;form action=\"index_ok.php\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    \u4e0a\u4f20\u56fe\u7247\uff1a&lt;input type=\"file\" name=\"pic\"&gt;\n    &lt;input type=\"submit\" name=\"sub\" value=\"\u4e0a\u4f20\"&gt;\n&lt;\/form&gt;\n&lt;table width=\"500\"&gt;\n    &lt;tr bgcolor=\"#cccccc\"&gt;\n    &lt;th&gt;\u5e8f\u53f7&lt;\/th&gt;\n    &lt;th&gt;\u56fe\u7247&lt;\/th&gt;\n    &lt;th&gt;\u6dfb\u52a0\u65f6\u95f4&lt;\/th&gt;\n    &lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;\n    &lt;\/tr&gt;\n    &lt;?php\n    \/\/1 \u6253\u5f00\u76ee\u5f55\n    $dir=opendir(\".\/uploads\");\n    \/\/2 \u904d\u5386\u76ee\u5f55\uff0c\u8f93\u51fa\u91cc\u9762\u7684\u56fe\u7247\n    $i=0;\n    while($f=readdir($dir)){\n        $i++;\n        \/\/\u8bfb\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff0c\u6a21\u62df\u6570\u636e\u5e93\u64cd\u4f5c\n        if($f!=\".\" &amp;&amp; $f!=\"..\"){\n            $filename=\".\/uploads\/$f\";\n            echo \"&lt;tr&gt;\";\n            echo \"&lt;td&gt;{$i}&lt;\/td&gt;\";\n            echo \"&lt;td&gt;&lt;img src='.\/uploads\/{$f}' width='80' height='60'&gt;&lt;\/td&gt;\";\n            echo \"&lt;td&gt;\".date(\"Y-m-d\",filectime($filename)).\"&lt;\/td&gt;\";\n            echo \"&lt;td&gt;&lt;a href='.\/uploads\/{$f}'&gt;\u67e5\u770b&lt;\/a&gt;&lt;\/td&gt;\";\n            echo \"&lt;\/tr&gt;\";\n        }\n    }\n    \/\/3 \u5173\u95ed\u76ee\u5f55\n    closedir($dir);\n    ?&gt;\n&lt;tr bgcolor=\"#cccccc\"&gt;&lt;td colspan=\"4\"&gt;&amp;nbsp;&lt;\/td&gt;&lt;\/tr&gt;\n&lt;\/table&gt;\n&lt;\/div&gt;&lt;\/pre&gt;\n\u00a0\n\n<\/code><\/pre>\n<h2>upload.php<\/h2>\n<pre><code class=\"PHP\">&lt;pre class=\"lang:php decode:true \"&gt;&lt;?php\nheader(\"Content-Type:text\/html;charset=utf-8\");\n    if($_FILES['pic']['error']&gt;0){\n        echo $_FILES['pic']['error'];\n        echo \"\u4e0a\u4f20\u6587\u4ef6\u9519\u8bef\uff01\";\n        echo '&lt;meta http-equiv=\"refresh\" content=\"3;url=index.php\"&gt;';  \/\/\u81ea\u52a8\u8df3\u8f6c\u56deindex\u6587\u4ef6\n    }else{\n        \/\/\u5f00\u59cb\u83b7\u53d6\u4e0a\u4f20\u6587\u4ef6\u7684\u4fe1\u606f\n        $file=$_FILES['pic'];\n        \/\/var_dump($file);\u6253\u5370\u6587\u4ef6\u91cc\u7684\u5168\u90e8\u4fe1\u606f\n        \/\/name:\u4e0a\u4f20\u6587\u4ef6\u540d\n        \/\/type:\u4e0a\u4f20\u6587\u4ef6\u7684\u7c7b\u578b\n        \/\/tmp_name:\u4e0a\u4f20\u6210\u529f\u540e\u7684\u4e34\u65f6\u6587\u4ef6\n        \/\/size:\u4e0a\u4f20\u6587\u4ef6\u7684\u5927\u5c0f\n        \/\/error:\u4e0a\u4f20\u6587\u4ef6\u7684\u9519\u8bef\u4fe1\u606f\n        $uploaddir=\".\/uploads\/\";  \/\/\u9009\u62e9\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u5b58\u653e\u76ee\u5f55\n        \/\/$uploadfile=$uploaddir.basename($file['name']);\/\/\u83b7\u5f97\u4e0a\u4f20\u6587\u4ef6\u7684\u540d\u79f0\n        \/\/\u89e3\u6790\u6587\u4ef6\u7684\u540d\u5b57\n        $fileinfo=pathinfo($file['name']);\n\/\/      echo $fileinfo['extension'];  \u83b7\u53d6\u6587\u4ef6\u7684\u7c7b\u578b\n        do{\n            $newfile=date(\"YmdHis\").rand(1000,9999).\".\".$fileinfo['extension'];\/\/\u66f4\u6539\u6587\u4ef6\u7684\u540d\u5b57\uff0c\u83b7\u53d6\u4e00\u4e2a\u65b0\u7684\u540d\u5b57\n        }while(file_exists($uploaddir.$newfile));\n\n        \/\/\u4e0a\u4f20\u6587\u4ef6\u7684\u7c7b\u578b\u9650\u5236\n        if (!(($file['type'] == \"image\/gif\")||($file['type'] == \"image\/jpeg\")||($file['type'] == \"image\/pjpeg\"))){\n            die(\"\u6587\u4ef6\u7c7b\u578b\u9519\u8bef\uff01\");\n            echo '&lt;meta http-equiv=\"refresh\" content=\"3;url=index.php\"&gt;';\n        }\n        \/\/\u4e0a\u4f20\u6587\u4ef6\u7684\u5927\u5c0f\u9650\u5236\n        if($file['size'] &gt; 2*1024*1024){\n            die(\"\u4e0a\u4f20\u6587\u4ef6\u8d85\u8fc72MB\uff01\");\n            echo '&lt;meta http-equiv=\"refresh\" content=\"3;url=index.php\"&gt;';\n        }\n        \/\/\u5f00\u59cb\u4e0a\u4f20\u6587\u4ef6\n        if (is_uploaded_file($file['tmp_name'])) {\n            if (move_uploaded_file($file['tmp_name'], $uploaddir.$newfile)) {\n                echo \"\u4e0a\u4f20\u6210\u529f\uff01\";\n                echo '&lt;meta http-equiv=\"refresh\" content=\"3;url=index.php\"&gt;';\/\/\u81ea\u52a8\u8df3\u8f6c\u56deindex\u6587\u4ef6\n            } else {\n                echo \"\u4e0a\u4f20\u5931\u8d25\uff0c\u8bf7\u7a0d\u7b49\uff01\";\n                echo '&lt;meta http-equiv=\"refresh\" content=\"3;url=index.php\"&gt;';  \/\/\u81ea\u52a8\u8df3\u8f6c\u56deindex\u6587\u4ef6\n            }\n        }\n\n    }\n?&gt;&lt;\/pre&gt;\n\u00a0\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7814\u7a76\u4e86\u4e00\u4e0bPHP\u7684FILES\u51fd\u6570\uff0c\u4f7f\u7528PHP\u5b9e\u73b0\u672c\u5730\u56fe\u7247\u7684\u4e0a\u4f20\uff0c\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u4e3a\u56fe\u7247\uff0c\u5e76\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u6307\u5b9a\u76ee\u5f55\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/09\/php-article44\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[34],"tags":[57],"class_list":["post-481","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-php","tag-php","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":1496,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions\/1496"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/482"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}