Click here to Skip to main content
15,884,769 members
Articles / Programming Languages / PHP

JavaScript/PHP - Multiple File Upload

Rate me:
Please Sign up or sign in to vote.
2.70/5 (26 votes)
15 Jul 2007CPOL1 min read 194.1K   5.4K   28   46
Option to add n number of file input controls to upload multiple files.

Introduction

This snippet allows users to add multiple files to be uploaded. By default, only one file upload control is visible. Upon clicking the "+" icon, more controls are added.

Screenshot - default.jpg

Default form

Screenshot - more.jpg

More file controls added

Background

Very often to allow multiple file uploads, an unnecessary number of file upload controls are preloaded in forms. This code is an example of how to keep the form clean without losing the important features.

Using the Code

This code snippet is developed with PHP and needs an Apache webserver to run. The "uploads" directory needs file read-write permission. To use it in an application, you will need to add a database interface to store the uploaded information. It is a single file that has both the form and its handler.

The script has two functions: uploadForm and upload. uploadForm displays the form and upload does the form handling.

PHP
<?php
if($_POST['pgaction']=="upload")
 upload();
else
 uploadForm();
?>

The above is an example for new programmers on how to deine a form and its handling in the same script.

HTML
<tr class="txt">
   <td valign="top">
    <div id="dvFile">
     <input type="file" name="item_file[]">
    </div>
   </td>
   <td valign="top">
    <a href="javascript:_add_more();" title="Add more">
     <img src="plus_icon.gif" border="0">
    </a>
   </td>
</tr>

The hyperlink to the icon invokes the JavaScript function _add_more, which adds more control to the form. I have used the div to identify a block of the form and populate more controls in this block.

JavaScript
function _add_more() {
  var txt = "<br><input type=\"file\" name=\"item_file[]\">";
  document.getElementById("dvFile").innerHTML += txt;
}

The JavaScript function adds more controls as required.

JavaScript
if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
  $GLOBALS['msg'] = ""; //initiate the global message
  for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { 
  //loop the uploaded file array
   $filen = $_FILES["item_file"]['name']["$j"]; //file name
   $path = 'uploads/'.$filen; //generate the destination path
   if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) {
   //upload the file
    $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>";
    //Success message
   }
  }
 }
 else {
  $GLOBALS['msg'] = "No files found to upload"; //No file upload message 
}

The uploaded file handler function checks for uploaded files, and if found, stores them in the web server.

Future Enhancement

In the code, if the user chooses a file and then adds more controls, the previous file information is not stored and is lost. I need to add a feature to store the previously chosen file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Hi,
I am a web-application developer and like to play around with different web technologies for fun. I already dug some ground in JavaScript, PHP, Java, MySQL, Apache and Linux and now like to dig some in C#. I will try to share some of my experience with you, which you may or may not like. Please advice suggestion to improve them.

Comments and Discussions

 
GeneralRe: Clears selections when adding additional fields Pin
truonglq17-Aug-09 0:17
truonglq17-Aug-09 0:17 
Questionwhat if the file uploaded has same file name? Pin
arriojb8-Feb-09 5:07
arriojb8-Feb-09 5:07 
AnswerRe: what if the file uploaded has same file name? Pin
AmgedOsman28-Aug-10 22:38
AmgedOsman28-Aug-10 22:38 
GeneralThanks Pin
thebirdman198412-Dec-08 3:13
thebirdman198412-Dec-08 3:13 
GeneralRe: Thanks Pin
old*vine3-Aug-09 14:05
old*vine3-Aug-09 14:05 
Questionproblem with firefox? Pin
arelanz29-Apr-08 16:16
arelanz29-Apr-08 16:16 
AnswerRe: problem with firefox? Pin
Jono Stewart19-Jun-08 17:31
Jono Stewart19-Jun-08 17:31 
GeneralRe: problem with firefox? Pin
arelanz22-Jun-08 14:34
arelanz22-Jun-08 14:34 
I've tried put quotation marks around file as u suggested but to no avail Dead | X| . FYI, the '[]' is used to make a particular HTML DOM element as an array..
I reverted to using browser detection technique, where if the user is using IE then he will have the dynamic upload fields, while firefox users will have predetermined set of upload fields (i set it to 10 only).
anyway thanx 4 da reply Suspicious | :suss:
GeneralRe: problem with firefox? Pin
Jono Stewart22-Jun-08 14:50
Jono Stewart22-Jun-08 14:50 
GeneralGood One Pin
Vishu Gurav16-Jul-07 5:21
Vishu Gurav16-Jul-07 5:21 
GeneralASP.NET?! Pin
Adam Tibi16-Jul-07 5:01
professionalAdam Tibi16-Jul-07 5:01 
GeneralRe: ASP.NET?! Pin
shirley_tzeling23-Jul-07 14:52
shirley_tzeling23-Jul-07 14:52 
QuestionHow about this? Pin
Vasudevan Deepak Kumar15-Jul-07 21:25
Vasudevan Deepak Kumar15-Jul-07 21:25 
GeneralExcellent!!! Pin
abhijithere15-Jul-07 21:08
abhijithere15-Jul-07 21:08 
GeneralCool... Pin
Sujoy G15-Jul-07 20:27
Sujoy G15-Jul-07 20:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.