Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-10-13
9
326 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:Simon336697
9 Comments
 
LVL 5

Assisted Solution

by:jericotolentino
jericotolentino earned 30 total points
ID: 22700827
See example three of the PHP documentation on handling file uploads:

http://www.php.net/file_upload
0
 
LVL 17

Accepted Solution

by:
psimation earned 440 total points
ID: 22700837
Well, just duplicate the code for more instances of the file input boxes...

I've started to give you an idea of what I mean in the code snippet - just carry on doing the same - ie, make 4 more $ext{2...5} where you substitute $filename for the $filename2...#filename5 etc.

most importantly is the 4 extra "if" statements for each of the new files:

if(move_uploaded_file($_FILES['userfile2']['tmp_name'],$upload_path . $filename)){
}
if(move_uploaded_file($_FILES['userfile3']['tmp_name'],$upload_path . $filename)){
}
etc.


=========================================== 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="file" name="userfile2" id="file"><br />
<input type="file" name="userfile3" id="file"><br />
<input type="file" name="userfile4" id="file"><br />
<input type="file" name="userfile5" 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'];
$filename2 = $_FILES['userfile2']['name'];
$filename3 = $_FILES['userfile3']['name'];
$filename4 = $_FILES['userfile4']['name'];
$filename5 = $_FILES['userfile5']['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

0
 
LVL 15

Assisted Solution

by:babuno5
babuno5 earned 30 total points
ID: 22700945
may be you can try this also for multiple file uploads which requires flash

http://www.element-it.com/DEMOMULTIPOW.aspx
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Simon336697
ID: 22700957
Hi psimation,
Thanks for that.
Is there any way instead of duplicating the code, to loop through files for multiple input boxes?
0
 
LVL 17

Expert Comment

by:psimation
ID: 22701023
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

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22701333
psimation, thats great...thanks so much for your skillful answers...really appreciate it psimation.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22701336
Thanks also to:
jericotolentino and babuno5
0
 
LVL 17

Expert Comment

by:psimation
ID: 22701359
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.
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31505551
Thank you guys :>)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
What's the best way to programatically have a MySQL DB update another MySQL DB 4 67
How do I show metrics with PHP or Javascript 6 32
Ajax and PHP 4 30
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question