Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does a multiple file upload work?

Coding

How does a multiple file upload work?

by  dpk136  Posted    (Edited  )
A lot of times, people want to have a form that has multiple uploads. meaning that they want to have the user upload more than one file at a time. i found that the way below is the easiest way to do this. it just uses a simple "For" loop in php. I have included some comments in the code below to explain what each step is doing.

Code:
<?php 
  $numoffile = 5; //number of files to upload
  [i]//this is the directory you would like the files to go into 
  // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder [/i]
  $file_dir  = "/var/www/uploaded";   
  if ($_POST) 
  { 
    [i]//if it is a post, loop through the $numoffile[/i]
    for ($i=0;$i<$numoffile;$i++) { 
      if (trim($_FILES['myfiles']['name'][$i])!="") { 
        [i]//creates the new file name with trimmed spaces[/i]
        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
        [i]//moves uploaded file from the tmp directory to the new directory with the file name[/i]
        move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile); 
        $j++;
      } 
    } 
  } 
  if (isset($j)&&$j>0) 
  {
    echo ("Your file(s) has been uploaded.<br>");
  } 
    echo ("<form method='post' enctype='multipart/form-data'>"); 
  for($i=0;$i<$numoffile;$i++) { 
    echo ("<input type='file' name='myfiles[]' size='30'><br>"); 
  } 
  echo ("<input type='submit' name='action' value='Upload'>"); 
  echo ("</form>"); 
?>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top