{"id":753,"date":"2012-06-18T13:28:30","date_gmt":"2012-06-18T13:28:30","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=753"},"modified":"2012-07-13T06:24:17","modified_gmt":"2012-07-13T06:24:17","slug":"create-multiple-file-uploader-using-php-html-and-jquery","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/create-multiple-file-uploader-using-php-html-and-jquery\/","title":{"rendered":"Create multiple file uploader using php, html and jQuery."},"content":{"rendered":"<p>When you have to upload an unknown number of files with php, adding some <strong>jQuery<\/strong> script can make your job a lot easier than you think. On this post I\u2019ll show you how to do that in no time and effortless. <\/p>\n<p>First, let\u2019s create the html form and add two \u201cbuttons\u201d that will be used for adding or removing an uploading field from the form. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;span class=&quot;add_field&quot;&gt;+&lt;\/span&gt;\r\n&lt;span class=&quot;remove_field&quot;&gt;-&lt;\/span&gt;\r\n\r\n&lt;form action=&quot;upload.php&quot; method=&quot;POST&quot; enctype=&quot;multipart\/form-data&quot;&gt;\r\n\t&lt;div class=&quot;input_holder&quot;&gt;\r\n\t\t&lt;input type=&quot;file&quot; name=&quot;uploaded_files[]&quot; id=&quot;input_clone&quot;\/&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;input type=&quot;submit&quot; value=&quot;upload_files&quot; \/&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>The span elements will be out add\/remove buttons so we\u2019ll add a listener to them to know when someone want to add or remove a field from the form.<\/p>\n<p>Because it\u2019s an uploading form, we have to specify that to the browser. We do this by adding <strong>enctype=&#8221;multipart\/form-data&#8221;<\/strong> as an attribute to the form. <\/p>\n<p>Also we add a file input type which has a unique id which will allow us to clone the element as many times as we want. The name of the field is given as an array in order to be able to stock up multiple data information without being forced to stock them with different names or add numbers or any other way of messing up the code.<\/p>\n<p>After this, let\u2019s add the listener on the buttons so the \u201cunknown number of fields\u201d to be feasible.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\"> \r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\r\n        $('.add_field').click(function(){\r\n\t\r\n            var input = $('#input_clone');\r\n            var clone = input.clone(true);\r\n            clone.removeAttr ('id');\r\n            clone.val('');\r\n            clone.appendTo('.input_holder'); \r\n\t\t\r\n        });\r\n\r\n        $('.remove_field').click(function(){\r\n\t\t\r\n            if($('.input_holder input:last-child').attr('id') != 'input_clone'){\r\n                  $('.input_holder input:last-child').remove();\r\n            }\r\n\t\t\r\n        });\r\n\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Now let\u2019s explain this part.<\/p>\n<p>When the user clicks the &#8216;.add_field&#8217; button we have to do several actions. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar input = $('#input_clone');\r\n<\/pre>\n<p>Here we get the first input of the field by its id.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar clone = input.clone(true);\r\n<\/pre>\n<p>We clone the element.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclone.removeAttr ('id');\r\n<\/pre>\n<p>Remove the id attribute. We don\u2019t want on the same page to have 2 or more elements with the same id.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclone.val('');\r\n<\/pre>\n<p>Here we remove the value of the input. This way we make sure that the user will not upload, by mistake, the same file twice or several times.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclone.appendTo('.input_holder'); \r\n<\/pre>\n<p>And last but not least we add the input to the div that holds them.<\/p>\n<p>After we finished with the adding part, maybe the user abused too much of the &#8220;+&#8221; button and there are way too many input fields. Let\u2019s make possible for him to remove them.<\/p>\n<p>First thing first here, we have to make sure that he does not abuse of the &#8220;-&#8221; button and remove all input files. We test if the input it&#8217;s not the original one (has the unique id), and if doesn&#8217;t have it we remove it.<\/p>\n<p>And now, our last step of this article is the php file that handles the uploading.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php \r\n\r\nif(isset($_FILES ['uploaded_files']))\r\n{\r\n     foreach($_FILES['uploaded_files']['name'] as $key=&gt;$value)\r\n     {\r\n          if(is_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key]) &amp;&amp; $_FILES[['uploaded_files']['error'][$key] == 0)\r\n          {\r\n              \t$filename = $_FILES['uploaded_files']['name'][$key];\r\n                $filename = time().rand(0,999).$filename;\r\n                            \r\n                if(move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key], 'uploads\/'. $filename))\r\n                {\r\n               \t      echo 'The file '. $_FILES['uploaded_files']['name'][$key].' was uploaded successful';\r\n                }\r\n                else\r\n                {\r\n                      echo 'There was a problem uploading the picture.';\r\n                } \r\n          }\r\n          else\r\n          {\r\n   \t        echo 'There is a problem with the uploading system.';\r\n          }\r\n     }\r\n}\r\n\r\n?&gt;\r\n<\/pre>\n<p>Here, first time you check if there are any files that need to be uploaded, by checking if the <strong>$_FILES<\/strong> variable is set. If there are any files ready for upload, then for each file you have to check if the file was successfully uploaded and if there are no errors. If so, your next step is to make sure your file has a unique name. On this I use a little trick renaming my file based on the current time and a random number and the filename. This way, the possibility of 1 or more users to upload a file with the same name is almost, if not, 0.<\/p>\n<p>The last step is to check if the file was moved on the upload folder. If everything goes well you will get, for each file, a message that will tell you that the file was successfully uploaded.<\/p>\n<p> There are a few problems that might occur and you can avoid them by:<br \/>\n1.\tMake sure the uploading folder exists. The <a href=\"http:\/\/php.net\/move_uploaded_file\">move_uploaded_file()<\/a> function will not automatically create it for you.<br \/>\n2.\tIf you want you can set a limit for the files. This limit has to be equal or lesser than <strong>post_max_size<\/strong> located in your <strong>php.ini<\/strong> file.<br \/>\n3.\tThe uploading folder has to have the permission for writing on.<\/p>\n<p>You can <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2012\/06\/uploadmultiplefiles.zip\">download<\/a> the file.<\/p>\n<p>If you have something to say about this subject please feel free to use the comment form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Create multiple file uploader using php, html and jQuery.\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/create-multiple-file-uploader-using-php-html-and-jquery\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>When you have to upload an unknown number of files with php, adding some jQuery script can make your job a lot easier than you think. On this post I\u2019ll show you how to do that in no time and effortless. First, let\u2019s create the html form and add two \u201cbuttons\u201d that will be used &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/create-multiple-file-uploader-using-php-html-and-jquery\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/create-multiple-file-uploader-using-php-html-and-jquery\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[86,105,35],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/753"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":26,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions\/785"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}