Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Turning this upload script from "1 file upload capable" to "multi-file upload capable"

Hi everyone hope you are all well.
Guys I have the following php script, that currently is only 1-file-upload capable.
Id like to modify it so that it is mulit-file upload capable....let's say 5 at a time.

I have included the script in the code snippet.
Any help greatly appreciated.
=========================================== upload.php:
 
<!-- ******************************************************
HTML FORM SECTION:
*********************************************************** -->
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> 
      <input type="file" name="userfile" id="file"><br />
      <input type="submit" value="Submit" />
   <p>
</form>
 
 
<!-- ******************************************************
UPLOAD SECTION:
*********************************************************** -->
 
<?php
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 					
      $max_filesize = 524288; 																
      $upload_path = './files/'; 															
 
   	$filename = $_FILES['userfile']['name']; 											
   	$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
 
  		if(!in_array($ext,$allowed_filetypes))
     		die('The file you attempted to upload is not allowed.');
 
   	if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      	die('The file you attempted to upload is too large.');
 
   	if(!is_writable($upload_path))
      	die('You cannot upload to the specified directory, please CHMOD it to 777.');
   	
   	if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; 
      else
         echo 'There was an error during the file upload.  Please try again.'; 
  
?>

Open in new window

SOLUTION
Avatar of jericotolentino
jericotolentino
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

Hi psimation,
Thanks for that.
Is there any way instead of duplicating the code, to loop through files for multiple input boxes?
Yes, you could put the current code into a foreach loop and step through each $_POST variable to get the names of the form fields, but you would then need to call your file input boxes something unique/easy to check for, since $_POST will contain all form elements.

So, you could do something like this maybe:





foreach($_POST as $key=>$val) {
 
if (($val == "userfile1")||($val=="userfile2")||($val=="userfile3")||($val=="userfile4")||($val=="userfile5")){
$filename = $_FILES[$val]['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
 
if(!in_array($ext,$allowed_filetypes))
 die('The file you attempted to upload is not allowed.');
 
if(filesize($_FILES[$val]['tmp_name']) > $max_filesize)
 die('The file you attempted to upload is too large.');
 
if(!is_writable($upload_path))
 die('You cannot upload to the specified directory, please CHMOD it to 777.');
       if(move_uploaded_file($_FILES[$val]['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; 
      else
         echo 'There was an error during the file upload.  Please try again.';
}
} 

Open in new window

psimation, thats great...thanks so much for your skillful answers...really appreciate it psimation.
Thanks also to:
jericotolentino and babuno5
No Problem Simon

Just remember to close the question by accepting one or more of the comments as solutions - else the question will remain open on the system.
Thank you guys :>)