{"id":768,"date":"2016-08-30T15:59:38","date_gmt":"2016-08-30T15:59:38","guid":{"rendered":"http:\/\/punjabirevolution.com\/?page_id=768"},"modified":"2016-08-30T16:16:51","modified_gmt":"2016-08-30T16:16:51","slug":"php-file-upload-simple-example","status":"publish","type":"page","link":"http:\/\/punjabirevolution.com\/index.php\/php-file-upload-simple-example\/","title":{"rendered":"PHP &#8211; File Upload Simple Example"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/cWSL94Uh570?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Uploading a file involves two steps.<\/p>\n<p>In first step, we create a form with attribute method=&rdquo;post&rdquo;. It does not work with get method. We also add another attribute enctype=&rdquo;multipart\/form-data&rdquo; to the form tag. This attribute is basically to encrypt the file contents. After that we add a input field with type file and a submit.<\/p>\n<p>In second step, we use php to move the file to desired folder. When we submit the form, it uploads the file to some temporary location $_FILES[&ldquo;myFile&rdquo;][&ldquo;tmp_name&rdquo;]. And from that temporary location we move the file to our desired directory.<\/p>\n<p>The sample code for the first step is as below.<\/p>\n<pre class=\"lang:php decode:true\">html&gt;\r\n\r\n&lt;body&gt;\r\n&lt;form action=\"fileUpload_action.php\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n\r\n&lt;input type=\"file\" name=\"myFile\" \/&gt;\r\n&lt;br&gt;\r\n&lt;input type=\"submit\" value=\"Submit\" &gt;\r\n\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Line 6: It would render a browse&nbsp; button which open the browser window.<\/p>\n<p>Following is the code of action page.<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\necho $_FILES[\"myFile\"][\"name\"];\r\necho \"&lt;br&gt;\";\r\necho $_FILES[\"myFile\"][\"tmp_name\"];\r\n\r\n\r\nmove_uploaded_file($_FILES[\"myFile\"][\"tmp_name\"],\"images\/\" . $_FILES[\"myFile\"][\"name\"] );\r\n\r\n?&gt;<\/pre>\n<p>Line 2: The FILES is a built in 2 d associative array. The first key of this array is the name of the input field of file type that was defined in previous name. the second key &ldquo;name&rdquo; would give the name of the uploaded file.<\/p>\n<p>Line 4: the tmp_name is used to get the current temporary path where the file was temporarily uploaded.<\/p>\n<p>Line 7: the built in php function to move the file from given temporary location to desired folder. the first argument is source and the second argument is destination. You can change the destination path and name whatever you desire.<\/p>\n<p>Happy Coding!!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Uploading a file involves two steps. In first step, we create a form with attribute method=&#8221;post&#8221;. It does not work with get method. We also add another attribute enctype=&#8221;multipart\/form-data&#8221; to the form tag. This attribute is basically to encrypt the file contents. After that we add a input field with type file and a &hellip; <a href=\"http:\/\/punjabirevolution.com\/index.php\/php-file-upload-simple-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP &#8211; File Upload Simple Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/768"}],"collection":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":7,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/768\/revisions"}],"predecessor-version":[{"id":776,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/768\/revisions\/776"}],"wp:attachment":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/media?parent=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}