Saturday, May 26, 2012

multiple file upload php



Multiple File Upload into single folder

Make Folder Named "upload" in your root directory Then you  have to make HTML form to submit uploaded files into upload folder in your root directory.Files I have created for this purpose are "mupload.html","mupload.php".

Below shows you the file upload interface created from "mupload.html" pressing Add another link creates another upload field in to the interface.

Multiple File Upload

Add another
Following codes generate the above file called "mupload.html".

<html>
<head>
 <title>multiple file upload </title>
<script type="text/javascript">
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);
}
</script>
</head>
<body>
 <form action="mupload.php" method="post" enctype="multipart/form-data" name="mutiple_file_upload_form" id="mutiple_file_upload_form">
 <h3>Multiple File Upload </h3>
<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>
</body>
 </html>

Below shows you the"mupload.php"  file which carries the uploaded file into folder upload in your root directory.

<?php
 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__) . '/upload/';
 for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
 $number_of_file_fields++;
 if ($_FILES['images']['name'][$i] != '') {
 $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);
}
 ?>

1 comment:

  1. Great peace of code, any possibility to show how to place this files into mysql database?, it makes it slightly complicated because of the multiple fields other wise it would be straight forward.

    Thanks any way.

    ReplyDelete