hi there... i have a form where a user may optionally upload a maximum of 2 files along with other textual data (there are 2 file fields built in the form). i'm having trouble writing a php script that will allow 2 files to be uploaded optionally.
i have successfully written a small php script that will allow one file upload (with one file field in the form), as well as uploading 2 files (with 2 file fields in the form). but i don't know how to make it optional. it seems that if the file field is there the user MUST upload something or else there's an error...
here is my HTML form code:
here is my PHP script:
i have successfully written a small php script that will allow one file upload (with one file field in the form), as well as uploading 2 files (with 2 file fields in the form). but i don't know how to make it optional. it seems that if the file field is there the user MUST upload something or else there's an error...
here is my HTML form code:
Code:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <form enctype="multipart/form-data" action="uploadtest.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> Choose a file to upload:<br /> <input name="datafileA" type="file" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> Choose a file to upload:<br /> <input name="datafileB" type="file" /> <br /><br /> <input type="submit" value="SUBMIT" /> </form> </body> </html>
Code:
<?php function UploadFiles() { $FileA = $_FILES['datafileA']; $FileB = $_FILES['datafileB']; if (($FileA != "") && ($FileB == "")) { UploadA(); } else if (($FileA == "") && ($FileB != "")) { UploadB(); } else if (($FileA != "") && ($FileB != "")) { UploadAandB(); } else { echo 'No Files Uploaded'; } } function UploadA() { $newfolder = "uploads/"; $uploadA = $newfolder . basename( $_FILES['datafileA']['name']); if (move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA)) { echo 'File A Uploaded'; } else { echo 'Error'; } } function UploadB() { $newfolder = "uploads/"; $uploadB = $newfolder . basename( $_FILES['datafileB']['name']); if (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadA)) { echo 'File B Uploaded'; } else { echo 'Error'; } } function UploadAandB() { $newfolder = "uploads/"; $uploadA = $newfolder . basename( $_FILES['datafileA']['name']); $uploadB = $newfolder . basename( $_FILES['datafileB']['name']); if ((move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA)) && (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadB))) { echo 'File A and B Uploaded'; } else { echo 'Error'; } } ?>
Comment