{"id":704,"date":"2014-04-15T06:04:56","date_gmt":"2014-04-15T10:04:56","guid":{"rendered":"http:\/\/www.searchenginegenie.com\/programming-blog\/?p=704"},"modified":"2014-04-15T06:14:22","modified_gmt":"2014-04-15T10:14:22","slug":"how-to-upload-a-file-or-image-from-html-form-using-php","status":"publish","type":"post","link":"https:\/\/www.searchenginegenie.com\/programming-blog\/how-to-upload-a-file-or-image-from-html-form-using-php\/","title":{"rendered":"How to Upload a image from HTML form using PHP?"},"content":{"rendered":"<p>[php]<br \/>\n&lt;?php<\/p>\n<p>if(isset($_REQUEST[&#8216;sub&#8217;]))<br \/>\n {<br \/>\n $fileType = $_FILES[&#8216;img&#8217;][&#8216;type&#8217;];<br \/>\n $fileSize = $_FILES[&#8216;img&#8217;][&#8216;size&#8217;];<br \/>\n if($fileSize\/1024 &gt; &#8216;2048&#8217;)<br \/>\n {<br \/>\n echo &#8216;Filesize is not correct it should be equal to 2 MB or less than 2 MB.&#8217;;<br \/>\n exit();<br \/>\n } \/\/FileSize Checking<br \/>\n if($fileType != &#8216;image\/png&#8217; &amp;&amp; $fileType != &#8216;image\/gif&#8217; &amp;&amp; $fileType != &#8216;image\/jpg&#8217; &amp;&amp; $fileType != &#8216;image\/jpeg&#8217; )<br \/>\n {<br \/>\n echo &#8216;Sorry this file type is not supported we accept only. Jpeg, Gif, PNG&#8217;;<br \/>\n exit();<br \/>\n } \/\/file type checking ends here.<br \/>\n $upFile = &#8216;uploads\/&#8217;.date(&#8216;Y_m_d_H_i_s&#8217;).$_FILES[&#8216;img&#8217;][&#8216;name&#8217;];<br \/>\n if(is_uploaded_file($_FILES[&#8216;img&#8217;][&#8216;tmp_name&#8217;])) {<br \/>\n if(!move_uploaded_file($_FILES[&#8216;img&#8217;][&#8216;tmp_name&#8217;], $upFile)) {<br \/>\n echo &#8216;Problem could not move file to destination.&#8217;;<br \/>\n exit;<br \/>\n }<br \/>\n } else {<br \/>\n echo &#8216;Problem: Possible file upload attack. Filename: &#8216;;<br \/>\n echo $_FILES[&#8216;img&#8217;][&#8216;name&#8217;];<br \/>\n exit;<br \/>\n }<br \/>\n $img= $upFile;<br \/>\n } \/\/File upload ends here.<\/p>\n<p>}<br \/>\n ?&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h3&gt;File Upload:&lt;\/h3&gt;<br \/>\nSelect a file to upload:<br \/>\n&lt;form action=&quot;&quot; enctype=&quot;multipart\/form-data&quot; method=&quot;post&quot;&gt;<br \/>\n&lt;input type=&quot;file&quot; name=&quot;img&quot; size=&quot;50&quot; \/&gt;<br \/>\n&lt;input type=&quot;submit&quot; name=&quot;sub&quot; value=&quot;Upload File&quot; \/&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/php]<\/p>\n<h3>File Upload:<\/h3>\n<p>Select a file to upload:<\/p>\n<form action=\"\" enctype=\"multipart\/form-data\" method=\"post\"><input type=\"file\" name=\"img\" size=\"50\" \/><input type=\"submit\" name=\"sub\" value=\"Upload File\" \/><\/form>\n","protected":false},"excerpt":{"rendered":"<p>[php] &lt;?php if(isset($_REQUEST[&#8216;sub&#8217;])) { $fileType = $_FILES[&#8216;img&#8217;][&#8216;type&#8217;]; $fileSize = $_FILES[&#8216;img&#8217;][&#8216;size&#8217;]; if($fileSize\/1024 &gt; &#8216;2048&#8217;) { echo &#8216;Filesize is not correct it should be equal to 2 MB or less than 2 MB.&#8217;; exit(); } \/\/FileSize Checking if($fileType != &#8216;image\/png&#8217; &amp;&amp; $fileType != &#8216;image\/gif&#8217; &amp;&amp; $fileType != &#8216;image\/jpg&#8217; &amp;&amp; $fileType != &#8216;image\/jpeg&#8217; ) { echo &#8216;Sorry this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":7,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/704\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}