How do I upload a HTML form with a username, password, multiple file uploads and then process it with PHP?

StackOverflow https://stackoverflow.com/questions/137730

  •  02-07-2019
  •  | 
  •  

Question

How to post a username, password and multiple binary files from a single html form and process it using php? I'm not allowed to use ajax.

Was it helpful?

Solution

first off check out these pages on PHP.net

  1. file upload info
  2. move_uploaded_file

But to get you started here's a couple stub files.

uploadForm.html

<html>
<body>
    <form action="processStuff.php" enctype="multipart/form-data" method="POST">
        username: <input type="text" name="username" />
        password: <input type="password" name="password" />

        <p>
            <input type="file" name="uploadFile[]" /><br />
            <input type="file" name="uploadFile[]" /><br />
            <input type="file" name="uploadFile[]" /><br />
            <!-- Add as many of these as you want -->
        </p>

        <p>
            <input type="submit" />
        </p>
    </form>
</body>
</html>

processStuff.php

<pre>
<?php

    echo '<h2>Username & password</h2>'
    echo "Username: {$_POST['username']}\nPassword: {$_POST['password']}";
    echo '<hr />';

    echo '<h2>Uploaded files</h2>'  
    foreach($_FILES['uploadFile']['tmp_name'] as $i => $tempUploadPath) {
        if (empty($tempUploadPath)) {
            // this <input type="file" /> was "blank"... no file selected
        } else {
            // a file was uploaded
            echo '<strong>A file named "', $_FILES['uploadFile']['name'][$i], "\" was uploaded</strong>\n";
            echo "\ttemporarily stored at: ", $tempUploadPath, "\n";            
            echo "\tmime type: ", $_FILES['uploadFile']['type'][$i], "\n";
            echo "\tsize: ", $_FILES['uploadFile']['size'][$i], " bytes\n";         
            echo "\terror code", 
            ((empty($_FILES['uploadFile']['size'][$i]) 
                    ? '<em>no errror</em>' 
                    : $_FILES['uploadFile']['size'][$i])), 
            "\n\n";

    // do something useful with the uploaded file 
        // access it via $tempUploadPath and use move_uploaded_file() to move 
        //     it out of the temp path before you manipulate it in any way!!!!!
        // see http://us3.php.net/features.file-upload
        // and http://us3.php.net/manual/en/function.move-uploaded-file.php
        }       
    }

?>
</pre>

The HTML file shows how to set the enctype of the <form> & the second form show you how to access the submitted username & password & finally how to loop thru every uploaded file.

As noted you MUST move the file(s) ASAP. They're uploaded to a temp location and the system will delete them unless you deal with them. So move 'em somewhere first then do whatever you need w/ them.

Hoep this helps

Arin

OTHER TIPS

You should use the $_FILES superglobal and move_uploaded_file() function to see which files were uploaded successfully and move them to their final location in case they were.

The $_POST superglobal will contain the submitted username and password.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top