views:

1749

answers:

4

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.

+1  A: 

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.

Vinko Vrsalovic
+2  A: 

http://www.tizag.com/phpT/fileupload.php

+5  A: 

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

arin sarkissian
thanks you so much this is exactly what i needed.
lajos
lajos
A: 

I think pointing the questioner to a Tizag.com link is quite appropriate here. Gave me a chuckle, actually. Perfect level of complexity for them.

gaoshan88