Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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"]; 

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


    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •