
{"id":6028,"date":"2017-04-17T02:16:15","date_gmt":"2017-04-17T02:16:15","guid":{"rendered":"http:\/\/tapchicntt.com\/?p=6028"},"modified":"2023-09-18T15:34:23","modified_gmt":"2023-09-18T08:34:23","slug":"huong-dan-upload-file-len-server-trong-php","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/huong-dan-upload-file-len-server-trong-php\/","title":{"rendered":"H\u01b0\u1edbng d\u1eabn Upload file l\u00ean Server trong PHP"},"content":{"rendered":"\n<p>V\u1edbi php b\u1ea1n d\u1ec5 d\u00e0ng upload file l\u00ean server. Tuy nhi\u00ean n\u00f3 c\u0169ng k\u00e8m theo nh\u1eefng m\u1ed1i nguy hi\u1ec3m cho h\u1ec7 th\u1ed1ng n\u00ean b\u1ea1n c\u1ea7n ph\u1ea3i c\u1ea9n th\u1eadn x\u1eed l\u00fd khi upload file l\u00ean server.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">C\u1ea5u h\u00ecnh \u0111\u1ec3 c\u00f3 th\u1ec3 upload file l\u00ean server trong php<\/h2>\n\n\n\n<p>C\u1ea5u h\u00ecnh file Php.ini<\/p>\n\n\n\n<p>\u0110\u1ea7u ti\u00ean b\u1ea1n ch\u1eafc ch\u1eafn php c\u1ee7a m\u00ecnh \u0111\u00e3 c\u1ea5u h\u00ecnh cho ph\u00e9p upload file<br>B\u1ea1n t\u00ecm v\u00e0o file php.ini t\u00edm \u0111\u1ebfn d\u00f2ng file_uploads v\u00e0 thi\u1ebft l\u1eadp tr\u1ea1ng th\u00e1i on<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfile_uploads = On\n<\/pre><\/div>\n\n\n<p>T\u1ea1o form upload b\u1eb1ng html<\/p>\n\n\n\n<p>T\u1ea1o form cho ph\u00e9p ng\u01b0\u1eddi s\u1eed d\u1ee5ng ch\u1ecdn file \u1ea3nh m\u00e0 h\u1ecd mu\u1ed1n upload<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Upload file to server&lt;\/title&gt;\n        &lt;meta charset=&quot;utf-8&quot;&gt;\n    &lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;wrapper&quot;&gt;\n        &lt;form method=&quot;POST&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n            &lt;input type=&quot;file&quot; name=&quot;fileUpload&quot;  id=&quot;fileUpload&quot; &gt;\n            &lt;input type=&quot;submit&quot; name=&quot;submit&quot; &gt;\n        &lt;\/form&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p><strong>L\u01b0u \u00fd:<\/strong><br>&#8211; Form h\u1ed7 tr\u1ee3 Upload khi ph\u01b0\u01a1ng th\u1ee9c truy\u1ec1n d\u1eef li\u1ec7u l\u00e0 POST<br>&#8211; Khai b\u00e1o thu\u1ed9c t\u00ednh <strong>enctype=&#8221;multipart\/form-data&#8221;<\/strong> cho From<\/p>\n\n\n\n<p>X\u1eed l\u00fd khi upload form<\/p>\n\n\n\n<p>\u1ede b\u01b0\u1edbc tr\u00ean ta \u0111\u00f3 c\u00f3 m\u1ed9t form c\u00f3 th\u1ec3 upload r\u1ed3i. \u1ede b\u01b0\u1edbc n\u00e0y ch\u00fang ta c\u00f9ng t\u00ecm hi\u1ec3u khi m\u00ecnh nh\u1ea5n submit th\u00ec \u0111\u1ec1u g\u00ec x\u1ea3y ra.<\/p>\n\n\n\n<p>\u1ede form c\u00f3 up d\u1ee5ng upload khi ch\u00fang ta ch\u1ecdn file v\u00e0 nh\u1eadn submit, ngay l\u1eadp t\u1ee9c d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c truy\u1ec1n v\u00e0o trong bi\u1ebfn h\u1ec7 th\u1ed1ng ch\u00ednh l\u00e0 $_FILES.<\/p>\n\n\n\n<p>Ch\u00fang ta s\u1eed d\u1ee5ng \u0111o\u1ea1n m\u00e3 sau \u0111\u1ec3 in ra th\u00f4ng tin m\u1ea3ng $_FILES<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif ($_SERVER&#x5B;&#039;REQUEST_METHOD&#039;] == &quot;POST&quot;) {\n    echo &quot;&lt;pre&gt;&quot;;\n    print_r($_FILES&#x5B;&#039;fileUpload&#039;]);\n    echo &quot;&lt;\/pre&gt;&quot;;\n}\n<\/pre><\/div>\n\n\n<p>T\u00f4i \u0111\u00e3 ch\u1ecdn up m\u1ed9t file \u1ea3nh v\u00e0 \u0111\u00e2y l\u00e0 k\u1ebft qu\u1ea3 c\u1ee7a m\u1ea3ng $_FILES<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nArray\n(\n    &#x5B;name] =&gt; picture.jpg\n    &#x5B;type] =&gt; image\/jpeg\n    &#x5B;tmp_name] =&gt; D:\\xampp\\tmp\\phpC225.tmp\n    &#x5B;error] =&gt; 0\n    &#x5B;size] =&gt; 57911\n)\n<\/pre><\/div>\n\n\n<p>Nh\u01b0 ch\u00fang ta th\u1ea5y m\u1ea3ng $_FILES hi\u1ec7n t\u1ea1i c\u00f3 c\u00e1c tr\u01b0\u1eddng nh\u01b0:<br>&#8211; name: T\u00ean file<br>&#8211; type: Th\u00f4ng tin \u0111\u1ecbnh d\u1ea1ng file(img, doc&#8230;)<br>&#8211; tmp_name: \u0110\u01b0\u1eddng d\u1eabn t\u1ea1m ch\u01b0a file chu\u1ea9n b\u1ecb \u0111\u01b0a l\u00ean Server<br>&#8211; error: L\u1ed7i khi upload file<br>&#8211; size: \u0110\u1ed9 l\u1edbn c\u1ee7a file(kb)<\/p>\n\n\n\n<p>C\u00e1c b\u01b0\u1edbc \u0111\u01b0a file \u0111\u01b0\u1ee3c upload l\u00ean Server<\/p>\n\n\n\n<p>Trong \u0111a s\u1ed1 tr\u01b0\u1eddng h\u1ee3p file ch\u00fang ta upload l\u00ean server ch\u00ednh l\u00e0 h\u00ecnh \u1ea3nh(H\u00ecnh \u1ea3nh \u0111\u1ea1i di\u1ec7n cho s\u1ea3n ph\u1ea9m, tin t\u1ee9c&#8230;)<\/p>\n\n\n\n<p>V\u00ec th\u1ebf n\u00ean ch\u00fang ta c\u1ea7n th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 b\u01b0\u1edbc tr\u01b0\u1edbc khi chuy\u1ec3n file \u0111\u00e3 \u0111\u01b0\u1ee3c ch\u1ecdn l\u00ean Server<\/p>\n\n\n\n<p>&#8211; Ki\u1ec3m tra \u0111u\u00f4i m\u1edf r\u1ed9ng c\u1ee7a file c\u00f3 n\u1eb1m trong c\u00e1c \u0111u\u00f4i h\u00ecnh \u1ea3nh kh\u00f4ng<br>&#8211; Ki\u1ec3m tra k\u00edch th\u01b0\u1edbc c\u1ee7a file chu\u1ea9n b\u1ecb up c\u00f3 \u0111\u1ea3m b\u1ea3o dung l\u01b0\u1ee3ng cho ph\u00e9p kh\u00f4ng<br>&#8211; Ki\u1ec3m tra xem file chu\u1ea9n b\u1ecb up c\u00f3 t\u1ed3n t\u1ea1i tr\u00ean h\u1ec7 th\u1ed1ng ch\u01b0a r\u1ed3i up l\u00ean<br>Tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u sau \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 h\u00e0m c\u1ea7n l\u01b0u \u00fd<\/p>\n\n\n\n<p>L\u1ea5y t\u00ean file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nbasename($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;]);\n?&gt;\n<\/pre><\/div>\n\n\n<p>L\u1ea5y \u0111u\u00f4i m\u1edf r\u1ed9ng c\u1ee7a file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n    pathinfo($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;], PATHINFO_EXTENSION);\n?&gt;\n<\/pre><\/div>\n\n\n<p>B\u01b0\u1edbc 1: T\u1ea1o \u0111\u01b0\u1eddng d\u1eabn file sau khi \u0111\u01b0\u1ee3c upload<br>\u1ede b\u01b0\u1edbc n\u00e0y ch\u00fang ta c\u1ea7n t\u1ea1o m\u1ed9t folder \u1edf Server \u0111\u1ec3 ch\u01b0a file v\u00e0 bi\u1ebft l\u1ea1i \u0111\u01b0\u1ee3c d\u1eabn \u0111\u1ebfn \u0111\u00f3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n    $target_dir = &quot;uploads\/&quot;;\n?&gt;\n<\/pre><\/div>\n\n\n<p>T\u00f4i \u0111\u00e3 t\u1ea1o m\u1ed9t bi\u1ebfn $target_dir l\u01b0u l\u1ea1i \u0111\u01b0\u1eddng folder c\u1ee7a t\u00f4i l\u00e0 uploads<br>Sau khi c\u00f3 \u0111\u01b0\u1ee3c th\u01b0 m\u1ee5c ch\u00fang ta t\u1ea1o s\u1eb5n \u0111\u01b0\u1eddng d\u1eabn c\u1ee7a file sau khi \u0111\u01b0\u1ee3c upload<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php \n    $target_dir = &quot;uploads\/&quot;; \n    $target_file = $target_dir.basename($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;]);\n?&gt;\n<\/pre><\/div>\n\n\n<p>V\u00ed d\u1ee5 t\u00f4i up file c\u00f3 t\u00ean l\u00e0 picture.jpg th\u00ec k\u1ebft qu\u1ea3 c\u1ee7a bi\u1ebfn $target_file nh\u01b0 sau<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuploads\/picture.jpg\n<\/pre><\/div>\n\n\n<p>B\u01b0\u1edbc 2: Ki\u1ec3m tra file tr\u01b0\u1edbc khi upload<br>Ki\u1ec3m tra \u0111u\u00f4i m\u1edf r\u1ed9ng file<br>V\u00ed d\u1ee5 ch\u00fang ta mu\u1ed1n up h\u00ecnh \u1ea3nh th\u00ec t\u1eadp h\u1ee3p \u0111u\u00f4i m\u1edf r\u1ed9ng ch\u00fang ta cho ph\u00e9p l\u00e0<br>&#8211; png<br>&#8211; jpg<br>&#8211; jpeg<br>&#8211; gif<br>\u0110\u1ec3 ki\u1ec3m tra file m\u00ecnh ch\u1ecdn up l\u00ean server c\u00f3 n\u1eb1m trong danh s\u00e1ch tr\u00ean hay kh\u00f4ng ta l\u00e0m nh\u01b0 sau<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n$typeFile = pathinfo($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;], PATHINFO_EXTENSION);\n$typeFileAllow = array(&#039;png&#039;,&#039;jpg&#039;,&#039;jpeg&#039;, &#039;gif&#039;);\nif(!in_array($typeFile, $typeFileAllow)){\n    $error = &quot;File b\u1ea1n v\u1eeba ch\u1ecdn h\u1ec7 th\u1ed1ng kh\u00f4ng h\u1ed7 tr\u1ee3, b\u1ea1n vui l\u00f2ng ch\u1ecdn h\u00ecnh \u1ea3nh&quot;;\n}\n?&gt;\n<\/pre><\/div>\n\n\n<p>Ki\u1ec3m tra k\u00edch th\u01b0\u1edbc file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n    $sizeFile = $_FILES&#x5B;&#039;size&#039;];\n    if($sizeFile &gt; 5242880){\n        $error = &quot;File b\u1ea1n upload kh\u00f4ng \u0111\u01b0\u1ee3c qu\u00e1 5MB&quot;;\n    }\n?&gt;\n<\/pre><\/div>\n\n\n<p>\u1ede tr\u00ean t\u00f4i quy \u0111\u1ecbnh dung l\u01b0\u1ee3ng file upload l\u00ean h\u1ec7 th\u1ed1ng kh\u00f4ng \u0111\u01b0\u1ee3c qu\u00e1 5MB~5242880bites<br>Ki\u1ec3m tra file upload \u0111\u00e3 t\u1ed3n t\u1ea1i tr\u00ean h\u1ec7 th\u1ed1ng ch\u01b0a<\/p>\n\n\n\n<p>Ch\u00fang ta s\u1eed d\u1ee5ng h\u00e0m file_exists()<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif(file_exists($target_file)){\n    $error = \"File b\u1ea1n ch\u1ecdn \u0111\u00e3 t\u1ed3n t\u1ea1i tr\u00ean h\u1ec7 th\u1ed1ng\";\n}\n<\/pre><\/div>\n\n\n<p>B\u01b0\u1edbc 3: Chuy\u1ec3n file \u1edf th\u01b0 m\u1ee5c t\u1ea1m l\u00ean Server<br>Sau khi l\u00e0m b\u01b0\u1edbc ki\u1ec3m tra ti\u00eau chu\u1ea9n file chu\u1ea9n b\u1ecb upload xong, n\u1ebfu nh\u01b0 file m\u00ecnh v\u1eeba ch\u1ecdn v\u01b0\u1ee3t qua \u0111\u01b0\u1ee3c b\u1ed9 l\u1ecdc th\u00ec ch\u00fang ta s\u1ebb chuy\u1ec3n file \u0111\u1ea5y l\u00ean server<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif(empty($error)){\n    if(move_uploaded_file($_FILES&#x5B;\"fileUpload\"]&#x5B;\"tmp_name\"], $target_file)){\n        echo \"B\u1ea1n \u0111\u00e3 upload file th\u00e0nh c\u00f4ng\";\n    }else {\n        echo \"File b\u1ea1n v\u1eeba upload g\u1eb7p s\u1ef1 c\u1ed1\";\n    }\n}\n<\/pre><\/div>\n\n\n<p>Ch\u00fang ta v\u1eeba k\u1ebft th\u00fac c\u00e1c b\u01b0\u1edbc th\u1ef1c hi\u1ec7n v\u00e0 \u0111\u00e2y l\u00e0 code \u0111\u1ea7y \u0111\u1ee7 PHP &amp; HTML:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nif ($_SERVER&#x5B;&#039;REQUEST_METHOD&#039;] == &quot;POST&quot;) {\n    $target_dir = &quot;uploads\/&quot;;\n    $target_file = $target_dir . basename($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;]);\n    $typeFile = pathinfo($_FILES&#x5B;&#039;fileUpload&#039;]&#x5B;&#039;name&#039;], PATHINFO_EXTENSION);\n    $typeFileAllow = array(&#039;png&#039;,&#039;jpg&#039;,&#039;jpeg&#039;, &#039;gif&#039;);\n    \n    if(!in_array(strtolower($typeFile), $typeFileAllow)){\n        $error = &quot;File b\u1ea1n v\u1eeba ch\u1ecdn h\u1ec7 th\u1ed1ng kh\u00f4ng h\u1ed7 tr\u1ee3, b\u1ea1n vui l\u00f2ng ch\u1ecdn h\u00ecnh \u1ea3nh&quot;;\n    }\n\n    $sizeFile = $_FILES&#x5B;&#039;size&#039;];\n    if($sizeFile &gt; 5242880){\n        $error = &quot;File b\u1ea1n ch\u1ecdn kh\u00f4ng \u0111\u01b0\u1ee3c qu\u00e1 5MB&quot;;\n    }\n\n    if(file_exists($target_file)){\n        $error = &quot;File b\u1ea1n ch\u1ecdn \u0111\u00e3 t\u1ed3n t\u1ea1i tr\u00ean h\u1ec7 th\u1ed1ng&quot;;\n    }\n\n    if(empty($error)){\n        if(move_uploaded_file($_FILES&#x5B;&quot;fileUpload&quot;]&#x5B;&quot;tmp_name&quot;], $target_file)){\n            echo &quot;B\u1ea1n \u0111\u00e3 upload file th\u00e0nh c\u00f4ng&quot;;\n        }  else {\n            echo &quot;File b\u1ea1n v\u1eeba upload g\u1eb7p s\u1ef1 c\u1ed1&quot;;\n        }\n    }\n} ?&gt;\n\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Upload file to server&lt;\/title&gt;\n        &lt;meta charset=&quot;utf-8&quot;&gt;\n    &lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;wrapper&quot;&gt;\n        &lt;form method=&quot;POST&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n            &lt;input type=&quot;file&quot; name=&quot;fileUpload&quot;  id=&quot;fileUpload&quot; &gt;\n            &lt;input type=&quot;submit&quot; name=&quot;submit&quot; &gt;\n        &lt;\/form&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>V\u1edbi php b\u1ea1n d\u1ec5 d\u00e0ng upload file l\u00ean server. Tuy nhi\u00ean n\u00f3 c\u0169ng k\u00e8m theo nh\u1eefng m\u1ed1i nguy hi\u1ec3m cho [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":4572,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212],"tags":[],"class_list":["post-6028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-thu-thuat"],"views":669,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/6028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=6028"}],"version-history":[{"count":30,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/6028\/revisions"}],"predecessor-version":[{"id":8709,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/6028\/revisions\/8709"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/4572"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=6028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=6028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=6028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}