[PHP] Multiple File Uploads

He has: 296 posts

Joined: May 2002

Hey,

I'm working on a review script for my friends site and I cannot for the life of me figure out how to do multiple file uploading. One file is easy, but having more than one I can't do. I tried to decipher the code used with Invision's File Manager but I couldn't.

Any help is greatly appreciated!

Thanks,
necrotic

[James Logsdon]

They have: 93 posts

Joined: Jun 2003

Well, what issue are you having with mutiple file uploads?

Are you trying to manage multiple types, or multiple actions etc.

If you cna provide more light on that then I think I can probably help you out.

BTW Im not familiar with Invision that much.

[Design Alpha] -Web Services : Design,Hosting,Advertising,Software
Ask about custom pricing on hosting!!
Site Assets: [UltraGaming.com] [Blades of Warcraft]

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Along these lines, I tried for ages to get them to work but kept screwing it up. Code samples would be great (maybe I'll figure out what I was doing wrong, too...)

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

This may not be perfect... I had to rip it out of a 449 line file with complex permissions built in.

<?php
// Absolute System Path (NO TRAILING SLASH)
$destination_dir = '/path/to/uploads';

// what is the max file size to accept? (in KB)
$MAX_FILE_SIZE = 200;

// how many files should we handle at once?
$num_files = 3;

// what extensions may they upload?
$extensions_ok = array (\"htm\",\"html\",\"bmp\",\"gif\",\"jpg\",\"png\");


// do the proper processing based on mode
if (
$mode=='u') {
   
    echo \"<font face='courier new' size=2>\n\";
   
    for (
$i=1; $i<=$num_files; $i++) {
        if (is_file(
$userfile[$i])) {
            // Pull out the information about this upload.
            // You don't need type or size unless you want them.
           
$filename = $userfile_name[$i];
           
$type = $userfile_type[$i];
           
$size = $userfile_size[$i];
           
$location = $userfile[$i];
           
           
$split_file = explode(\".\", $entry);
            if (in_array(
$split_file[count($split_file)-1], $extensions_ok)) {
                echo \"Cannot upload '
$filename'.  Not an allowed extension.\n\";
            }
            if (strstr(
$filename, \" \")) {
                echo \"Cannot upload '
$filename'.  Filename contains spaces.\n\";
            }
            else if(is_uploaded_file(
$userfile[$i])) {
                echo str_pad(\"Uploading '
$filename'\", 50, \".\", STR_PAD_RIGHT);
                if (copy(
$userfile[$i], $destination_dir.\"/$filename\")) {
                   
$tmp = \" OK<BR>\n\";
                }
                else {
                   
$tmp = \" Error<BR>\n\";
                }
                echo str_pad(
$tmp, 9, \".\", STR_PAD_LEFT);
            }
        } //END if (is_file(
$userfile[$i]))
    } //END for
   
    echo \"</font>\n\";
    echo \"<br>\n\";

    echo \"<FORM ENCTYPE=\\"
multipart/form-data\\" ACTION=\\"\".$PHP_SELF.\"\\" METHOD=\\"POST\\">\n\";
    echo \"<table border=0 cellspacing=0 cellpadding=0>\n\";
    echo \" <tr>\n\";
    echo \"  <td>\n\";
    echo \"    <font face=\\"
courier new\\" size=\\"2\\">\n\";
    echo \"    Upload file(s):<BR>\n\";
    echo \"    </font>\n\";
    echo \"  </td>\n\";
    echo \" </tr>\n\";
    echo \" <tr>\n\";
    echo \"  <td colspan=2>\n\";
   
    echo \"    <INPUT TYPE=\\"
hidden\\" name=\\"MAX_FILE_SIZE\\" value=\\"\".($MAX_FILE_SIZE*1024).\"\\">\n\";
    for (
$i=1; $i<=$num_files; $i++) {
        echo \"    <INPUT NAME=\\"
userfile[$i]\\" TYPE=\\"file\\" size=50><BR>\n\";
    }
    echo \"    <INPUT TYPE=\\"
hidden\\" NAME=\\"mode\\" VALUE=\\"u\\">\n\";
    echo \"    <INPUT TYPE=\\"
submit\\" VALUE=\\"Upload\\">\n\";
    echo \"  </td>\n\";
    echo \" </tr>\n\";
    echo \" <tr>\n\";
    echo \"  <td>\n\";
    echo \"    <font face='courier new' size=2><BR>\n\";
    echo \"    Max file size: \".
$MAX_FILE_SIZE.\"KB each<BR>\n\";
    echo \"    Valid file extensions:\n\";
    sort(
$extensions_ok);
    echo join(\", \",
$extensions_ok);
    echo \"    </font>\n\";
    echo \"  </td>\n\";
    echo \" </tr>\n\";
    echo \"</table>\n\";
    echo \"</FORM>\n\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Works like a charm. Thanks!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

*whew*

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.