Multiple file uploading tutorial with php
We already discussed about increasing file upload size and simple file upload in my previous posts.now we discuss about the logic behind the multiple file upload.
Here we create unlimited file fields on the fly with javascript ,so that there is no limit.
uploader.php:-we process all the uploaded files and show the results of uploaded files. remember to validate and filter the file types please view my previous post.
Please Download Source Code To View it
handling-multiple-file-upload-php
Javascript Code
1 2 3 4 5 6 7 8 9 | function add_file_field(){ var container=document.getElementById('file_container'); var file_field=document.createElement('input'); file_field.name='images[]'; file_field.type='file'; container.appendChild(file_field); var br_field=document.createElement('br'); container.appendChild(br_field); } |
HTML Code
1 2 3 4 5 6 7 8 | <form action="uploader.php" method="post" enctype="multipart/form-data" name="mutiple_file_upload_form" id="mutiple_file_upload_form">
<h1>Advanced Multiple File Upload Script Example</h1><div id="file_container">
<input name="images[]" type="file" />
<br />
</div>
<a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
<input type="submit" name="Submit" value="Submit" />
</form> |
PHP Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | if (isset($_POST['Submit'])) { $number_of_file_fields = 0; $number_of_uploaded_files = 0; $number_of_moved_files = 0; $uploaded_files = array(); $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory /** * we get a $_FILES['images'] array , * we procee this array while iterating with simple for loop * you can check this array by print_r($_FILES['images']); */ for ($i = 0; $i < count($_FILES['images']['name']); $i++) { $number_of_file_fields++; if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not $number_of_uploaded_files++; $uploaded_files[] = $_FILES['images']['name'][$i]; if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) { $number_of_moved_files++; } } } echo "Number of File fields created $number_of_file_fields.<br/> "; echo "Number of files submitted $number_of_uploaded_files . <br/>"; echo "Number of successfully moved files $number_of_moved_files . <br/>"; echo "File Names are <br/>" . implode(',', $uploaded_files); } |
Download Source Code
handling-multiple-file-upload-php
Screen Shots
Popularity: 14% [?]











Related Articles
No user responded in this post
Leave A Reply