Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 06:27 PM   PM User | #1
kan3xiao
New Coder

 
Join Date: Jun 2012
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
kan3xiao is an unknown quantity at this point
Help with multiple file upload form

Hi all,

I came across a pretty good file upload script that I have working perfectly with only one upload box in a form. If I want to have 2 upload boxes in one form, how would I have to adjust the variables in the script below to get it to work.

PHP Code:
$filename $_FILES["file"]["name"];
 
$file_basename substr($filename0strripos($filename'.')); // get file extention
 
$file_ext substr($filenamestrripos($filename'.')); // get file name
 
$filesize $_FILES["file"]["size"];
 
$allowed_file_types = array('.jpg','.gif','.png''.pdf');
   
   if (
in_array($file_ext,$allowed_file_types)  &&  ($filesize 1024000)) {
      
// rename file
        
$newfilename md5($file_basename) . $file_ext;
            if (
file_exists("images/" $newfilename)) {        
            
// file already exists error
                
$error[] =  "You have already uploaded this file.";            
            } else {        
                
move_uploaded_file($_FILES["file"]["tmp_name"], "images/" $newfilename);
                }
           } elseif (empty(
$file_basename)) {    
            
// file selection error
           
$error[] =   "Please select a file to upload.";        
        } elseif (
$filesize 200000) {    
            
// file size error
            
$error[] =  "The file you are trying to upload is too large.";        
        } else {    
            
// file type error
               
$error[] =  "Only these file types are allowed for upload: " implode(', ',$allowed_file_types);
            
unlink($_FILES["file"]["tmp_name"]);        
        } 
and the form in the HTML:

Code:
<form method="post" action="upload.php" id="uploadform" enctype="multipart/form-data">
<input type="file" name="file" id="file" size="50">
<input name="upload" type="submit" value="Upload Files">
</form>
I know I have to make another input box, so it would look something like this:

Code:
<form method="post" action="upload.php" id="uploadform" enctype="multipart/form-data">
<input type="file" name="file" id="file" size="50">
<input type="file" name="file2" id="file2" size="50">
<input name="upload" type="submit" value="Upload Files">
</form>
But then how would I duplicate and modify the PHP code above? I tried just duplicating the whole code and putting 2s after all the variables but of course that didn't work. Anyone have any ideas?
kan3xiao is offline   Reply With Quote
Old 10-23-2012, 11:54 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Try
Code:
<input type="file" name="file[]" id="file" size="50">
<input type="file" name="file[]" id="file2" size="50">
PHP Code:
foreach($_FILES as $key => $value){
$filename $value["name"];
 
$file_basename substr($filename0strripos($filename'.')); // get file extention
 
$file_ext substr($filenamestrripos($filename'.')); // get file name
 
$filesize $value["size"]; 

.............


__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 AM.


Advertisement
Log in to turn off these ads.