Results 1 to 4 of 4

Thread: Multiple File Uploader

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple File Uploader

    Hello, I am trying to make a script that will upload multiple files. I got it to show multiple fields and all, but when it uploads, it only uploads one of the files. Here is the code:
    PHP Code:
    <?php

    switch(@$_GET['step']) {
        default:
            
    $path basename(__FILE__)."?step=uploadfiles";
        echo 
    '
            <h3>Uploader</h3>
            
        <form action="'
    .$path.'" method="post" enctype="multipart/form-data">
            Number of fields: <input type="text" name="number" />
            
            <input type="submit" value="Send" />
        </form>
        '
    ;
        break;

        
    ###### Upload Files Case ######
        
        
    case 'uploadfiles':
            
    $total $_POST['number'];
            
        echo 
    "
        <form action="
    .basename(__FILE__)."?step=upload"." method=\"post\" enctype=\"multipart/form-data\">
            Folder Name: <input type=\"text\" name=\"dir\" />
                <br />
            Files: <br />
        "
    ;
        
        
    $i 0;
        while(
    $i != $total) {
            echo 
    "<input type=\"file\" name=\"files[]\" />
                <br />"
    ;
        
    $i++;
        }
        
        
    // End form
        
    echo '
            <input type="submit" value="send" />
        </form>
        '
    ;
        break;
        

    ###### Upload Case ######

        
    case 'upload':
            
            echo 
    '
        <h3>Uploader</h3>'
    ;
        
        foreach(
    $_FILES['files']['error'] as $key) {
            
    $dir $_POST['dir'];
            if(!
    is_dir($dir)) {
                
    mkdir($dir);
                
                if(!
    is_dir($dir)) {
                    die(
    'Can\'t Create Directory');
                }
            }
        
        if(
    is_dir($dir)) {
            
    $tmp_name $_FILES['files']['tmp_name'][$key];
            
    $name $_FILES['files']['name'][$key];
            
    move_uploaded_file($tmp_name$dir."/".$name);
            
            echo 
    "
                
    {$_POST['dir']}/$name - Uploaded Successfully
                    <br />"
    ;
        }
    }
    break;

    }

    ?>
    I believe the error is near the end around the foreach statement, but can't figure out what exactly it is. I appreciate any help. Thanks
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry for the late reply, but here is your code with the modified parts in it.

    Code:
    <?php
    
    switch(@$_GET['step']) {
        default:
            $path = basename(__FILE__)."?step=uploadfiles";
        echo '
            <h3>Uploader</h3>
            
        <form action="'.$path.'" method="post" enctype="multipart/form-data">
            Number of fields: <input type="text" name="number" />
            
            <input type="submit" value="Send" />
        </form>
        ';
        break;
    
        
    ###### Upload Files Case ######
        
        case 'uploadfiles':
            $total = $_POST['number'];
            
        echo "
        <form action=".basename(__FILE__)."?step=upload"." method=\"post\" enctype=\"multipart/form-data\">
            Folder Name: <input type=\"text\" name=\"dir\" />
                <br />
            Files: <br />
        ";
        
        $i = 0;
        while($i != $total) {
            echo "<input type=\"file\" name=\"files[]\" />
                <br />";
        $i++;
        }
        
        // End form
        echo '
            <input type="submit" value="send" />
        </form>
        ';
        break;
        
    
    ###### Upload Case ######
    
        case 'upload':
            
            echo '
        <h3>Uploader</h3>';
      
    foreach($_FILES['files']['name'] as $k => $v) {
    
            $dir = $_POST['dir'];
            if(!is_dir($dir)) {
                mkdir($dir);
                
                if(!is_dir($dir)) {
                    die('Can\'t Create Directory');
                }
            }
        
        if(is_dir($dir)) {
            $tmp_name = $_FILES['files']['tmp_name'][$k];
            $name = $_FILES['files']['name'][$k];
            move_uploaded_file($tmp_name, $dir."/".$name);
            
            echo "
                {$_POST['dir']}/$name - Uploaded Successfully
                    <br />";
        }
    
    }
    break;
    
    }
    
    ?>
    Notice the parts in red. Your script was simply uploading one file because the key was getting 0. After changing the first red line, you now get the correct key value (0,1,2, etc) as well as the name of the file. I have tested this script and it works (I got up to 10 files in one upload on one test).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, I changed it and it works great. So how come it needs to be "$k => $v"? Thanks for the help.
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    $k for "key", $v for "value". Before, all it was getting was the value. So I changed to items. The first was $_FILES['files']['error'] (I changed error to name), everytime the script ran this section of code, all of the values (or keys as you had defined it) were zeros. This is why it would only upload the first file and nothing else. I changed this to name, and got the key and value of the $_FILES['files'] array. After that, the key got assigned 0,1,2,... and the value was the name of each file being uploaded.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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